What best practices should be followed when integrating reCAPTCHA with PHP forms?

When integrating reCAPTCHA with PHP forms, it is important to follow best practices to ensure the security of your website. This includes verifying the reCAPTCHA response on the server side before processing the form submission. By validating the reCAPTCHA response in PHP, you can prevent bots from submitting the form and ensure that the user is human.

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

// Verify the reCAPTCHA response
if(isset($_POST['g-recaptcha-response'])){
    $captcha = $_POST['g-recaptcha-response'];
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha);
    $responseKeys = json_decode($response, true);
    if(intval($responseKeys["success"]) !== 1) {
        // reCAPTCHA verification failed, handle accordingly
        die("reCAPTCHA verification failed");
    } else {
        // reCAPTCHA verification successful, process the form submission
        // Your form processing logic here
    }
} else {
    // reCAPTCHA response not set, handle accordingly
    die("reCAPTCHA response not set");
}
?>