New Google reCaptcha Implementation in PHP

Introduction :

Google reCaptcha is one of the greatest spam and abuse defender who used by millions of webmasters. Spam robots may attack the site that don't implement reCaptcha. To attack websites, Spam robots will use the forms. Robots will use the forms for registering millions of fake accounts in a second. It will load both servers and database. The auto-form filling may avoid by Google reCaptcha. The first Captcha developed by Google needed to write some words to verify whether the form filler was human or robot, but the new Captcha didn't require to write anything to verify. All he want to do, just click on a checkbox to check. We can call the new reCaptcha as "no Captcha reCaptcha".

Introduction to New Google reCaptcha:


Steps to Implement reCaptcah in PHP:

It is very easy to implement reCaptcha on your website using PHP. To do these, You have to know the basic knowledge of Html and PHP. Without having the necessary knowledge, you can't do nothing. If you have the knowledge, just continue reading or just try to learn them first.

Step 1: Registering Site Key and Secret Key: 

The first step that you want to do that, you just need to create a Site Key and Secret Key. Without having that key, you can't create the reCaptcha. You can create the key by click on the following link address that given below.
Here I already coded a form in HTML that will help you to understand how the new reCaptcha was working. Create a new page and name it "index.php" Then paste the Html Form code below this paragraph and save it.
<html>
<head><title>Recaptcha Verification Text</title></head>
<script src='https://www.google.com/recaptcha/api.js'></script>
<body>
<form action="action.php" method="POST">
<label>Name : <input type="text" name="name">
<br>
<label>Gmail : <input type="text" name="gmail">
<br>
<input type="submit" value="Submit" name="submit">
<div class="g-recaptcha" data-sitekey="YOUR SITE KEY HERE"></div>
</form>
</body>
</html>
  • All you want to do that, just change the "YOUR SITE KEY HERE" with the Site Key, which got from Google reCaptcha Dashboard.

Step 3: Creation of Action Page to Perform the Action:

So when clicking the submit button in the HTML Contact Form, The "action.php" page will perform its work. Here the action was to display the name and email of the user. If the reCaptcha was an error or not ticked. The action page must display an error message instead of name and email. If the reCaptcha was correct, then the name and email of the user must be showed in the action page. Before creating an action page, you need to create a new page with the name "recaptchalib.php" and copy the code that I linked to Github Repository.
Now you want to copy and paste the code which given below to the "action.php" Page and save it. 
<?php
require_once "recaptchalib.php";
    if(isset($_POST['g-recaptcha-response']))
    $captcha=$_POST['g-recaptcha-response'];

        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
        if($response['success'] == false)
        {
          echo '<h2>You are a Robot</h2>';
        }
        else
        {
                        $name = $_POST['name'];
   $gmail = $_POST['gmail'];
   echo 'Your Name Is :'; echo $name;
                 echo '<br>';
                 echo 'Your Email Address Is :'; echo $gmail;
        }
?>
  • On the above "action.php" Code, you just have to replace the "SECRET KEY HERE" with the Secret Key you got from Google reCaptcha Dashboard. Then save the action page.
  • If the user successfully verified the reCaptcha, Then he can see the "Name" and "Email" on the action page.
  • If the user can't check the reCaptcha, Then the user will get a message "You are a Robot." 

Suggested Article for Php Developers:

I hope you well enjoyed this article. If you have any doubts related to this article. Please let us know by using the comment box given below. Thanks 

0 comments:

Post a Comment

Wapmaster or Webmaster can Comment Us with the following Comment Box . Don't add Links to your site for Spamming our Blog .. Once you add Comment with Link . It will be deleted immediatly by our Admins .

 
Top