QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Google’s No CAPTCHA reCAPTCHA PHP Code Example

Google’s No CAPTCHA reCAPTCHA PHP Code Example

recaptcha

Google recently launched a new way to prevent bots sending form requests. Old reCAPTCHA asked website users to fill a input box with text presented in an image. This old reCAPTCHA model was not user friendly and had become less secure as new algorithms were developed to break it. Therefore Google came out with a new reCAPTCHA model. The new model can verify spam or not by just using a checkbox. Now users don’t have to read the scary text and fill text boxes. The new reCAPTCHA model is called as “No CAPTCHA reCAPTCHA”.
The new model is more user friendly and secure.

Registering and Retrieving Keys

To integrate the new reCAPTCHA in your website forms first register your site and retrieve reCAPTCHA client and secret keys.

PHP Code Example

Integrating it is very easy. Here is a PHP code example of integrating in your forms

View Demo

<!doctype html>
<html>
    <head>
        <title>Google reCAPTCHA demo</title>
        <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
        <form method="post" action="index.php">
            <div class="g-recaptcha" data-sitekey=""></div>
            <input type="submit" />
        </form>
    </body>
</html>

<?php

   
    if($_SERVER["REQUEST_METHOD"] === "POST")
    {
        //form submitted

        //check if other form details are correct

        //verify captcha
        $recaptcha_secret = "";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true)
        {
            echo "Logged In Successfully";
        }
        else
        {
            echo "You are a robot";
        }
    }

Here assign data-sitekey attribute to site key and PHP recaptcha_secret variable to secret key.

Dec 4, 2014Narayan Prusty
Accessing User Phone Contacts using Intel XDKHow does Google’s No CAPTCHA reCAPTCHA work?
Comments: 27
  1. Bismarck
    5 years ago

    Thanks my code did not work, I only remove IP like you code and its WORKS

    ReplyCancel
  2. vikas
    5 years ago

    HI Thank you for posting this simple and easy to understand example, it really helped me a lot ! But what if i want it on Localhost ? btw i found a good article on it please visit
    this link

    ReplyCancel
  3. Ravi Sah
    5 years ago

    Hi nice post,
    Can you add a error message on it after clicking the button when captcha is not validate.

    Thanks!!

    ReplyCancel
  4. David
    6 years ago

    Always handle javascript for nocaptcha explicitly. It is explained here on How to integrate nocaptcha

    ReplyCancel
  5. Udaipur-taxi
    6 years ago

    nice suggestions we definitely work on that.
    thanks!

    ReplyCancel
  6. Patrick
    6 years ago

    No good at all. All it displays is a submit button without the captcha

    ReplyCancel
    • Avanish Kumar
      6 years ago

      I apply recaptcha in Node.js. Everyone can see code.

      http://j2sexample.blogspot.in/2016/04/google-recaptcha-using-javascript.html

      ReplyCancel
  7. Mahdi
    6 years ago

    So simple and so great!
    thanx!

    ReplyCancel
  8. udaipur taxi
    6 years ago

    Great to see it. Thanks for sharing.

    ReplyCancel
    • julie ranawat
      6 years ago

      Great site…It’s Really very informative for all…thank’s for sharing lovely blog…www.pendulumevents.com

      ReplyCancel
  9. Hemanta D
    6 years ago

    It was great tutorial. Thank you.

    ReplyCancel
    • Gopal
      6 years ago

      if you understand how its works can help me out this issue,.. below i’ve mention my query,.. i need to implement to server side validation like below, but i can;t get this,,….

      $captcha=$_POST[‘g-recaptcha-response’];
      $secret = ‘6LefkhMTAAAAAHkEnnJN7vYLIGgFaehT5o8WRtjL';
      $response = file_get_contents(“https://www.google.com/recaptcha/api/siteverify?secret=”.$secret.”&response=”.$captcha);
      $response = json_decode($response, true);
      if($response[“success”] === true)
      echo “Logged In Successfully”;
      else
      echo “You are a robot”;

      ReplyCancel
  10. Gopal
    6 years ago

    Any one can know this solution for this issue ?

    <script src='https://www.google.com/recaptcha/api.js'></script>
    <form iaction="" method="post">
    <input type="submit" name="Submit" value="Check"><br><br>
    <div class="g-recaptcha" data-sitekey="-------"></div>
    </form>

    $captcha=$_POST['g-recaptcha-response'];

    $secret = '6LefkhMTAAAAAHkEnnJN7vYLIGgFaehT5o8WRtjL';

    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha);

    $response = json_decode($response, true);

    if($response["success"] === true)
    echo "Logged In Successfully";
    else
    echo "You are a robot";

    hi, i’ve using above code, but continuously i’ve getting you are robot. $response[“success”] it not give any kind of output. but i’ve try to copy $url and hit google chrome output is {“success”:true}, i’ve getting nothing..

    ReplyCancel
    • Avanish Kumar
      6 years ago

      I apply recaptcha in Node.js. Everyone can see code.

      http://j2sexample.blogspot.in/2016/04/google-recaptcha-using-javascript.html

      ReplyCancel
  11. John
    6 years ago

    Thank You! This was much easier than I thought it was going to be. No separate php file required like the old recaptcha.

    ReplyCancel
  12. Mayur
    6 years ago

    Hi,
    I found the mistake it was the error for secure key.
    Thanks for posting this Its helps me more.

    Thanks,
    Mayur Dhudasia

    ReplyCancel
  13. Mayur
    6 years ago

    always received error message as “You are a robot”
    I have
    Used public key in javascript function
    Used private key at server side

    ReplyCancel
    • Amolie
      6 years ago

      Ya its great. www.jointclinic.in

      ReplyCancel
  14. Farai
    7 years ago

    //Help, mine is coming out as a blank page the moment I integrate the code you have provided as follows;

    ReplyCancel
  15. raj
    7 years ago

    ok this is good but how to change width of re-captcha in css.?

    ReplyCancel
    • Narayan Prusty
      7 years ago

      u cannot style the captcha because its rendered inside an iframe. But you can style the iframe container which is of class .g-recaptcha

      ReplyCancel
      • bajwn
        6 years ago

        Recaptcha is annoying they sould kill it with fire before it lays eggs

        ReplyCancel
  16. Manish
    7 years ago

    we have multiple php forms on our website. Each of these forms has got multiple validations such as phone numbers, email ids, and a captcha (not google captcha). Still we’re getting hundreds of spam emails through those forms everyday. Could you please suggest what should we do?

    ReplyCancel
  17. zombietimeshare
    7 years ago

    Apparently I am a robot.

    ReplyCancel
  18. junaid
    7 years ago

    i do not get the challenge image only the alternate text, any ideas on why this might be happening?

    ReplyCancel
    • Narayan Prusty
      7 years ago

      Can you provide a screenshot?

      ReplyCancel
      • ramesh
        5 years ago

        Awesome article man it help me lot

        thanks

        ReplyCancel

Leave a Reply to ramesh Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

Image7 years ago 28 Comments Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • Registering and Retrieving Keys
  • PHP Code Example
Related Articles
  • How does Google’s No CAPTCHA reCAPTCHA work?
  • How Does HTTP Authentication Work?
  • Login Using Facebook Tutorial
  • Create a Crawlable AJAX Website using PHP
  • Displaying And Highlighting Source code in HTML Page
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license