What are some common methods to prevent spamming in PHP guestbooks or forums?

One common method to prevent spamming in PHP guestbooks or forums is to implement a CAPTCHA system. This requires users to complete a challenge, such as typing in distorted text, before submitting their message. Another method is to use IP address filtering to block known spamming IP addresses. Additionally, implementing moderation features where messages are reviewed before being posted can help prevent spam.

// CAPTCHA implementation
session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['captcha'] == $_SESSION['captcha']) {
        // Valid captcha, process the form submission
    } else {
        // Invalid captcha, display an error message
    }
}

// Generate CAPTCHA code
$captcha = substr(md5(uniqid()), 0, 6);
$_SESSION['captcha'] = $captcha;

// Display CAPTCHA image
echo '<img src="captcha.php" alt="CAPTCHA">';
echo '<input type="text" name="captcha" placeholder="Enter CAPTCHA code" required>';
```

```php
// IP address filtering
$spam_ips = ['127.0.0.1', '192.168.0.1']; // Add known spamming IP addresses here

if (in_array($_SERVER['REMOTE_ADDR'], $spam_ips)) {
    // Block the submission and display an error message
} else {
    // Process the form submission
}
```

```php
// Moderation feature
$message = $_POST['message'];

// Check message for spam keywords
$spam_keywords = ['viagra', 'cialis', 'online casino']; // Add spam keywords here

foreach ($spam_keywords as $keyword) {
    if (stripos($message, $keyword) !== false) {
        // Flag the message for moderation
        $moderation_required = true;
        break;
    }
}

if ($moderation_required) {
    // Display a message indicating that the message is under review
} else {
    // Process the form submission
}