photo

Now we cover how to implement Google Captcha onto PHP Website

Spamming is one of the most common disturb re the web. Every site owner wants to profit rid of spamming whether it is spam traffic or spam messages or spam clarification on your site.

To acquire rid of spamming you mannerism to put some checks about your website. Previously people used to put some random number or string upon the screen and ventilate it upon the server side.

But this method is earliest days method. Googles reCAPTCHA is a involve ahead way to insist a fan. And it is the whole straightforward to assert the user. They just habit a single click or tap to prove they are not a robot.

Today we are going to implement google captcha on PHP website via core coding. Captcha is nothing but a system to idetified that the form filler or login person is a Human not a robot.

This look like this

Step 1 . Login to Google https://www.google.com/recaptcha/admin

 

Step 2 . get reCAPTCHA API key

You need to register your website and get an API key from google to use reCAPTCHA.

Register your website at – https://www.google.com/recaptcha/admin

 

Choose the type of captcha you want to use and the domain/domains for you will use this API key and click on the Register Button.

Now you will get your site key and secret key.

 

Step 3 . Adding reCAPTCHA to your site

To add reCAPTCHA first you need to include 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="your_site_key">

Replace your_site_key with the actual site key provided by the google.

Once you are done refresh your page and you will see the reCAPTCHA widget on your site.

Step 4 . validate the User Response

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

 

Advertisement