How to Integrate Google reCAPTCHA with PHP?

Integrate google reCAPTCHA with PHP - Knowband

Nowadays everyone wants to get rid of spam on their website whether it is spam comments or spam messages or spam traffic on your site. To protect your website from spam, one of the best and easiest ways is to use Google reCAPTCHA.

In earlier days, people used to put some random number or string on the screen and verify it on the server-side. But this method is the old-day method and it is also time taking for many users whereas Google’s reCAPTCHA method is easy to use and takes less time resulting in a better user experience. They just need a single click to prove that they are humans and not a robot. Google reCAPTCHA is very easy to implement in a PHP script.

Google reCAPTCHA

Recommended Read: How to integrate Google ReCaptcha protection on your PrestaShop store?

In this blog, you will learn how to Integrate Google reCaptcha with PHP in our step by step guide.

Learn how to Integrate Google reCaptcha with PHP in Knowband's step by step guide

Step 1: To fetch the Google reCAPTCHA API key

To get an API key to use reCAPTCHA with PHP, you need to register your website at – https://www.google.com/recaptcha/admin. The screenshots are given below for your help:

Fetch the Google reCAPTCHA API key - Knowband
Label: Use a label of your own choice, so that it is easy to recognize the site in the future. You can add your brand name of website URL in this field to enhance the readability of the project name.

reCAPTCHA type: Choose any one type of reCAPTCHA you prefer for your site. There are two types available currently i.e., reCAPTCHA v3 and reCAPTCHA v2. We have used reCAPTCHA v2 in the sample project.

Domains: Here you need to enter your website name for which you will be implementing the reCAPTCHA functionality. It applies to the domains you enter here, and also to its subdomains. For example, a registration for abc.com also registers xyz.abc.com.

Now just check the checkbox of Accept the reCAPTCHA Terms of Service and click on Submit button.

Now you have the site key and secret key for your website.
he site key and secret key for your website - Knowband

Step 2: To Implement the Google reCaptcha with PHP the second step comes as Implementing reCAPTHA functionality to your site:

  • To add reCAPTCHA in PHP first you need to include the reCAPTCHA JavaScript library in your HTML.
    <script src='https://www.google.com/recaptcha/api.js' async defer >
  • Now add this HTML code where you want to show the reCAPTCHA widget.
    <div class="g-recaptcha" data-sitekey="site_key">
  • Replace site_key with the site key provided by google for your website.
  • It is done now, just refresh your page and you will see the reCAPTCHA widget on your site.

Step 3: To Validate the user’s response

Now, you need to validate the response when a user clicks on the reCAPTCHA widget. The PHP code to verify the user’s response is given below:

<?php
 
  if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
  {
        $secret = 'your_actual_secret_key';
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success)
        {
            $succMsg = 'Your contact request have submitted successfully.';
        }
        else
        {
            $errMsg = 'Robot verification failed, please try again.';
        }
   }
?>

Add your actual secret key in the above code and you are Done.

Get the Sample code here.

Recommended Read:  Two Effective Modules to Keep Away Spammers and Bots from PrestaShop store

Google reCaptcha module available for Prestashop and Opencart by KnowbandKnowband offers Google ReCaptcha Addon for various platforms including PrestaShop, and OpenCart. The Google ReCaptcha module is a powerful security tool that guards your website against spam entries while facilitating easy access for actual users.

Knowband guide on google recaptcha plugin that can be used in various forms

The Google ReCaptcha plugin can be used for sign-in, registration, password reset, well-known contact forms, and other things. Images are shown in the Google ReCaptcha addon as part of their standard validation processes to help identify humans.

This Google reCaptcha module offers excellent performance and is extremely customizable. From the back end of Knowband modules, the e-merchant may quickly adjust the Google reCaptcha addon.

Knowband - Prestashop Google ReCaptcha Addon

Prestashop Google ReCaptcha Addon

PrestaShop Google reCaptcha by Knowband enhances your store’s security by integrating Google reCaptcha, preventing spam and automated bots.

SHOP NOW

Final Words:

Implementing Google reCAPTCHA on websites is essential for security. It helps tell real users from bots, preventing spam and automated attacks. This keeps user data safe and ensures the website runs smoothly. By blocking harmful traffic, reCAPTCHA improves the user experience, making online interactions safer and more reliable.

Recommended read – Empower Your PrestaShop Store Security with Knowband’s Top PrestaShop Modules
Along with this, the Google reCaptcha is also compatible with Knowband’s One Page Checkout, protecting you against transaction fraud. It is the best way to guard your store against transaction fraud. To view the admin demo and front-end demo of the Google reCaptcha Module, visit our store.

Avoid letting spammers harm your business!

Aparajita Singh

Aparajita Singh

Aparajita Singh is an experienced Software engineer in PHP. She also has knowledge of C, JAVA and database design. She has been working in the IT Industry from the last 2yrs and still looking forward to achieving more in the IT industry. She lives in New Delhi and her hobby is to write the technical writeups.

Leave a Reply

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