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 spam is a crucial part of any website. Nowadays, a lot of bots continuously send fake data through website forms. Adding reCAPTCHA on the website helps 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: If someone is looking to integrate reCAPTCHA in Laravel then read the article Add Google reCAPTCHA on Laravel Website Forms.

Register The Site And Get API Keys

First, you need to register your site with Google reCAPTCHA – 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 out the above form, you will get your Site key and Secret key. Through these keys, Google incorporates the reCAPTCHA service on your website.

Integrate Google reCAPTCHA

You are ready with your reCAPTCHA API keys. Next, install this package which allows you to add and validate reCAPTCHA. The 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 package, one can add the reCAPTCHA in the HTML form using the methods provided in the library.

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

<?php
require_once "vendor/autoload.php";
 
$sitekey = 'YOUR_SITE_KEY';
$secret  = 'YOUR_SECRET_KEY';
$captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey);
?>
 
<form method="POST">
    <p>
        <input type="email" name="email" placeholder="Enter email address" />
    </p>
    <?php echo $captcha->display(); ?>
    <button type="submit" name="submit">Submit</button>
</form>
<?php echo $captcha->renderJs(); ?>

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

If you inspect the page, you’ll notice these methods render to HTML elements like div to display reCAPTCHA and script to include JavaScript 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 with form
    } else {
        echo "Incorrect captcha";
    }
}

So our final code is as follows.

<?php
require_once "vendor/autoload.php";
 
$sitekey = 'YOUR_SITE_KEY';
$secret  = 'YOUR_SECRET_KEY';
$captcha = new \Anhskohbo\NoCaptcha\NoCaptcha($secret, $sitekey);
 
if ( isset($_POST['submit'])) {
    if($captcha->verifyResponse($_POST['g-recaptcha-response'])) {
        //proceed with form
    } else {
        echo "Incorrect captcha";
    }
}
?>
 
<form method="POST">
    <p>
        <input type="email" name="email" placeholder="Enter email address" />
    </p>
    <?php echo $captcha->display(); ?>
    <button type="submit" name="submit">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 *