Skip to content
PHP Logo

Are there any specific PHP libraries or packages recommended for implementing spam protection in online forms?

Spam protection in online forms is essential to prevent unwanted submissions and ensure the integrity of the data collected. One recommended PHP library for implementing spam protection is Google reCAPTCHA, which provides a user-friendly way to verify that the form submission is not generated by a bot.

php
// Include the reCAPTCHA library
require_once('recaptchalib.php');

// Your reCAPTCHA keys
$public_key = 'YOUR_PUBLIC_KEY';
$private_key = 'YOUR_PRIVATE_KEY';

// Verify the reCAPTCHA response
$recaptcha = new ReCaptcha($private_key);
$resp = $recaptcha->verifyResponse($_SERVER['REMOTE_ADDR'], $_POST['g-recaptcha-response']);

if (!$resp->isSuccess()) {
    // Handle invalid reCAPTCHA response
    die('reCAPTCHA verification failed.');
} else {
    // Process the form submission
    // Add code here to handle the form data
}