What are common pitfalls when integrating reCAPTCHA into a PHP contact form?

One common pitfall when integrating reCAPTCHA into a PHP contact form is not properly validating the reCAPTCHA response before processing the form submission. To solve this issue, ensure that you check if the reCAPTCHA response is valid before sending the form data.

<?php
// Your reCAPTCHA secret key
$secret = 'YOUR_RECAPTCHA_SECRET_KEY';

// Verify the reCAPTCHA response
$response = $_POST['g-recaptcha-response'];
$verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success = json_decode($verify);

if ($captcha_success->success) {
    // Process the form submission
    // Your code to handle the form data goes here
} else {
    // Display an error message or redirect back to the form
    echo "reCAPTCHA verification failed. Please try again.";
}
?>