How can Captcha be integrated into a PHP contact form effectively?

To integrate Captcha into a PHP contact form effectively, you can use Google reCAPTCHA. This helps prevent spam submissions and ensures that the form is being filled out by a human user. By adding the necessary code to validate the Captcha response before processing the form data, you can enhance the security of your contact form.

<?php
// Verify Captcha response
$recaptcha_secret = 'YOUR_RECAPTCHA_SECRET_KEY';
$recaptcha_response = $_POST['g-recaptcha-response'];

$verify_url = 'https://www.google.com/recaptcha/api/siteverify';
$verify_response = file_get_contents($verify_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$response_data = json_decode($verify_response);

if (!$response_data->success) {
    // Captcha verification failed
    echo 'Captcha verification failed. Please try again.';
} else {
    // Process form data
    // Your form processing code here
}
?>