Knowband Blog | Ecommerce Modules

How to Integrate google reCAPTCHA with PHP?

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.

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

In this blog, you will learn how to integrate google reCAPTCHA on your website with PHP step by step.

Step 1: To fetch the reCAPTCHA API key

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


Label: Use a label of your own choice, so that it is easy to recognize the site in the future.

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.

Step 2: Implement reCAPTHA functionality to 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

Knowband 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.

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.

Final Words:

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!