As we take a look on websites, we can easily see that most of them have contact form built in. But having this contact form could switch to huge amount of spam messages sent by using that form. Fortunatelly we can stop it using the Google reCAPTCHA on our website.

Register at Google reCAPTCHA site

First step to add Google reCAPTCHA to the website is to register your site at

After filling up the form, we receive a pair of secret keys and some details about integration.


As the first step of implementation we have to include reCAPTCHA JavaScript library on website.

<script src="" async defer></script>

Next step is to update our contact form. Just before the submit button we have to place following code, which will be responsible of rendering reCAPTCHA field.

<div class="g-recaptcha" data-sitekey="{Site key}"></div>

And that’s it on the frontend.

Let’s go to backend. Inside the logic which is responsible for form validation we have to add following snippet:

if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
    //your secret key
    $secret = '{Secret key}';
    //get verify response data
    $verifyResponse = file_ get_contents(''.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);
        //proceed the form fields validation and sending the e-mail
        //reCAPTCHA has not positive validation
    //reCAPTCHA has not be chacked

And that’s it. You can enjoy reCAPTCHA on your site and mailbox free of spam now.

Google reCAPTCHA – use with PHP
Tagged on:                 

Leave a Reply

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

Social Widgets powered by