How effective is the implementation of Captcha in preventing spam in PHP guestbooks?

The implementation of Captcha in PHP guestbooks is generally effective in preventing spam by requiring users to prove they are human before submitting a form. This helps to block automated bots from filling out guestbook forms with spam content. By incorporating Captcha, the likelihood of receiving spam submissions is significantly reduced.

<?php
session_start();

if(isset($_POST['submit'])){
    if(isset($_POST['captcha']) && $_POST['captcha'] == $_SESSION['captcha']){
        // Captcha validation successful, process form submission
    } else {
        // Captcha validation failed, display error message
        echo "Captcha verification failed. Please try again.";
    }
}

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

// Display Captcha image
echo '<img src="captcha.php" alt="Captcha Image">';
?>