How can PHP be used to implement a spam protection system in a guestbook?

Spam protection in a guestbook can be implemented using PHP by adding a captcha system to prevent automated submissions. This can help differentiate between human users and bots, reducing the amount of spam entries in the guestbook.

// PHP code to implement a captcha system for spam protection in a guestbook
session_start();

if(isset($_POST['submit'])){
    if($_POST['captcha'] == $_SESSION['captcha']){
        // Process the guestbook entry
        echo "Guestbook entry submitted successfully!";
    } else {
        // Display error message for incorrect captcha
        echo "Captcha verification failed. Please try again.";
    }
}

// Generate a random captcha code
$captcha = rand(1000,9999);
$_SESSION['captcha'] = $captcha;

// Display the captcha image
echo "<img src='captcha.php' alt='Captcha Image'>";
echo "<input type='text' name='captcha' placeholder='Enter Captcha'>";
echo "<input type='submit' name='submit' value='Submit'>";