Using Google reCAPTCHA On Your Website Forms With PHP

Are you looking to integrate Google reCAPTCHA into your website? Google provides a reCAPTCHA service which adds the “I’m not a robot” checkbox in your form to validate users. This reCAPTCHA protects your forms against bots. In this article, we study how to add and validate Google reCAPTCHA on a website using PHP.

Protecting your database against spams is a crucial part of any website. Nowadays, a lot of bots continuously send fake data through website forms. By adding reCAPTCHA on the website, it will help us to stop spam registrations, comments, abusive languages, etc.

Google reCAPTCHA is designed to find out whether an incoming request is from a real human or from a bot.

That being said, let’s see a step by step guide on adding and validating reCAPTCHA on your website.

Note: In past, we have written an article on adding reCAPTCHA in Laravel forms. If someone is looking to integrate reCAPTCHA in Laravel then please read the article Add Google reCAPTCHA on Laravel Website Forms.

Register The Site And Get API Keys

At first, you need to register your site here – https://www.google.com/recaptcha/admin. Choose the options for reCAPTCHA v2 and “I’m not a robot” checkbox as shown in the screenshot.

google-recaptcha

If you intend to test it on a local server then use the string ‘localhost’ in the domain field.

Upon filling the above form, you will get your Site key and Secret key.

Integrate Google reCAPTCHA

You are ready with your reCAPTCHA API keys. Next, install this GitHub Library which allow to add and validate reCAPTCHA. Recommended way of installing this library is through Composer.

Open the terminal in your project root directory and run the command:

composer require anhskohbo/no-captcha

After installing the library, we can add the reCAPTCHA in our form using the methods provided in the library.

For instance, let’s create a form that has fields like email, reCAPTCHA, and a submit button.

<?php
require_once "vendor/autoload.php";

$secret  = 'YOUR_SECRET_KEY';
$sitekey = 'YOUR_SITE_KEY';
$captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey);
?>

<form method="POST">
    <p>
        <input type="email" class="form-control" placeholder="Enter email address">
    </p>
    <?php echo $captcha->display(); ?>
    <button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<?php echo $captcha->renderJs(); ?>

Make sure to replace placeholders with the actual values. In the above code, function $captcha->display() displays the “I’m not a robot” checkbox. The method $captcha->renderJs() includes the required JS file of reCAPTCHA.

Now, to validate the user write the below code on the form submission.

if ( isset($_POST['submit'])) {
    if($captcha->verifyResponse($_POST['g-recaptcha-response'])) {
        //proceed further
    } else {
        echo "Incorrect captcha";
    }
}

So our final code is as follows.

<?php
require_once "vendor/autoload.php";

$secret  = 'YOUR_SECRET_KEY';
$sitekey = 'YOUR_SITE_KEY';
$captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey);

if ( isset($_POST['submit'])) {
    if($captcha->verifyResponse($_POST['g-recaptcha-response'])) {
        //proceed further
    } else {
        echo "Incorrect captcha";
    }
}
?>

<form method="POST">
    <p>
        <input type="email" class="form-control" placeholder="Enter email address">
    </p>
    <?php echo $captcha->display(); ?>
    <button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<?php echo $captcha->renderJs(); ?>

That’s it! I hope you got to know about integrating Google reCAPTCHA on your website forms. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “Using Google reCAPTCHA On Your Website Forms With PHP

Leave a Reply

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