How to set up recaptcha (no captcha) in PHP

3 steps to implement new recaptcha no captcha in PHP

1. Register the domain to get the keys – “site key” and “secret key”

2. include code in the page you want to show the recaptcha

a. in <head>

<script src='https://www.google.com/recaptcha/api.js'></script>

b. in <form> – exactly you want to show the recaptcha

<div class="g-recaptcha" data-sitekey="xxxxx"></div>

3. include code to check upon submission (PHP file that process the form)

// use this function to get true/false result from the submitted response
function captcha_verify($secret_key)
{
        $response =file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$_POST['g-recaptcha-response']);
        
$response = json_decode($response, true);
        if($response["success"] === true)
        {
            return true;
        }
        return false;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *