What are some common methods to prevent spam in a PHP guestbook?

Spam in a PHP guestbook can be prevented by implementing CAPTCHA verification, using honeypot fields, and filtering out common spam keywords. These methods help to ensure that only legitimate users can submit entries to the guestbook.

// CAPTCHA verification
session_start();
if(isset($_POST['submit'])){
    if($_POST['captcha'] == $_SESSION['captcha']){
        // Process form submission
    } else {
        // Display error message
    }
}

// Honeypot field
if(!empty($_POST['website'])){
    // Don't process form submission
}

// Filter out common spam keywords
$spamKeywords = array('viagra', 'cialis', 'porn');
foreach($_POST as $key => $value){
    foreach($spamKeywords as $keyword){
        if(stripos($value, $keyword) !== false){
            // Display error message
            break;
        }
    }
}