Google reCAPTCHA Tutorial

Google reCAPTCHA Tutorial
Google reCAPTCHA Tutorial
Google reCAPTCHA

Google has recently announced new service to prevent your website from spammers and attackers. It’s completely new reCAPTCHA API called “Are you a Robot?”. They named it “NO CAPTCHA reCAPTCHA”. It is designed to prevent your websites from attackers and spammer.

[button-blue url=”http://demos.eggslab.net/Google-reCAPTCHA/” target=”_blank” position=””]Live Demo[/button-blue][button-green url=”http://demos.eggslab.net/downloads/5″ target=”_blank” position=””]Download[/button-green]

In this tutorial we are going to learn “How to Integrate Google reCAPTCHA in your site?“.

Register Your Website to Get Secret Key

You need to register your website on Google reCAPTCHA to get Site Key and Secret Key for your website. You can access to Google reCAPTCHA from here.

Login to Google reCAPTCHA using your Google Account and submit form.

Google reCAPTCHA Form

When you submit you will get Site Key and Secret Key like:

Site Key
Site Key
Secret Key
Secret Key

Integrate Google reCAPTCHA into your Website

To integrate Google reCAPTCHA into your website you need to put it in Client Side as well as in Server side. In Client HTML page you need to integrate this line before <head> tag.

<script src='https://www.google.com/recaptcha/api.js'></script>

To put Google reCAPTCHA widget into your form add the following code below your form.

<div class="g-recaptcha" data-sitekey="<---Your site Key--->"></div>

Don’t forget to replace <—Your site Key—> with your Site Key.

When the form get submitted to Server, the above script will send ‘g-recaptcha-response’ as a POST data and then you need to verify whether user has checked the CAPTCHA or not.

Simple Comment Form to Demonstrate Google reCAPTCHA

Here is HTML Portion

<html>
    <head>
        <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
	    <h1>Google reCAPTCHA Tutorial</h1>
        <form action="" method="post">
            <label>Email</label>
            <div><input name="email" type="text" placeholder="Type Your Email" size="39" /></div><br>
            <label>Comment</label>
            <div><textarea name="comment" cols="40" rows="5" placeholder="Type You Comment..."></textarea></div><br>
            <div class="g-recaptcha" data-sitekey="<---Your Site Key--->"></div>
        </form>
    </body>
</html>

I am using PHP for Sever Side Scripting avd cialis review. So after form will submit we will check POST variable.

if(isset($_POST['submit']))
{
	$email=$_POST['email'];
	$comment=$_POST['comment'];
	$captcha=$_POST['g-recaptcha-response'];

	if(!$captcha)
	{
	  echo 'Please check the the captcha form.';
	}

	$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=<--Your Secret Key-->&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

	if($response.success==false)
	{
	  echo "Hey! Spammer I'm Using Google reCAPTCHA! Get Out";
	}
	else
	{
	  echo 'Thanks for posting comment.';
	}
}

Don’t forget to replace <–Your Secret Key–> with your own secret key.

For more documentation you can read from here.

[button-blue url=”http://demos.eggslab.net/Google-reCAPTCHA/” target=”_blank” position=””]Live Demo[/button-blue][button-green url=”http://demos.eggslab.net/downloads/5″ target=”_blank” position=””]Download[/button-green]

 


13 responses to “Google reCAPTCHA Tutorial”

  1. CB Avatar
    CB

    Great tutorial. Being a designer and not a programmer, where do I instruct the form or php to email to a certain email address? I also want a successful submission to open up a thank you page. Where would I code this? Tks 🙂

  2. Suhas Aggarwal Avatar
    Suhas Aggarwal

    Checkout – http://www.smileycaptcha.com . It is a next generation Captcha which does advertising, Analytics, Entertainment.

  3. Chris Avatar
    Chris

    Great post dude (Y)

  4. Rahul Aggarwal Avatar
    Rahul Aggarwal

    Very helpful post. Thanks for sharing

  5. Jonas Avatar

    Really great post. Especially for sample google-recaptcha.php file to download at the very beginning
    which made things much easy for me!!! 😀
    I tryed other tutorials but it was hard to make them work.
    Finally I found yours. It works perfeclty.
    Thanks a lot. Good luck.

  6. Israel Tiomno Avatar

    Thanks for this tutorial!

    Please, fix the code for PHP. The response from google reCAPTCHA API is actually a string in JSON format. So, I’d change the lines from 12 – 14 like this:

    $responseJSON =file_get_contents(“https://www.google.com/recaptcha/api/siteverify?secret=&response=”.$captcha.”&remoteip=”.$_SERVER[‘REMOTE_ADDR’]);

    $response = json_decode( $responseJSON );

    if ( ! $response->success )

    Enjoy your coding!

  7. Hassan Avatar
    Hassan

    Very good tutorial 😉

  8. babuchak jethya Avatar
    babuchak jethya

    nice post 🙂

  9. Stef Avatar
    Stef

    Hello, i just put the code on localhost. Why i get error notification like this?

    Notice: Use of undefined constant success – assumed ‘success’
    Call Stack

    on this line: if($response.success==false)

  10. Israel Tiomno Avatar
    Israel Tiomno

    Hi Stef, please check out my comment above. That should fix the issue.

  11. N A N. O Avatar

    How is this helpful???? where is you php code placed? is it a separate file? What’s the file name? How is it linked to the html code?

  12. Abdullah Majid Avatar

    Brother see code carefully you will get PHP code yourself 😉 It’s already there

Leave a Reply

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