How to Use reCAPTCHA with PHP

recaptcha with php

reCAPTCHA service is very popular all over the world and used by millions of application. Currently reCAPTCHA is acquired by Google and is very strong program to stop spammers. In this tutorial I’ll teach how to place CAPTCHA on your PHP site but before starting tutorial let start with definition of CAPTCHA.

What is CAPTCHA?

A CAPTCHA is a program that can tell whether its user is a human or computer (bots).  Colorful images with distorted text at the bottom of Web registration forms. CAPTCHAs are used by many websites to prevent abuse from “bots,” or automated programs usually written to generate spam. No computer program can read distorted text as well as humans can, so bots cannot navigate sites protected by CAPTCHAs.

Step 1: Download reCAPTCHA PHP Library

To use reCAPTCHA with php, you have to download reCAPTCHA PHP Library.

You will only need one file from there (recaptchalib.php)

Step 2: Get You API key

Next step is to signed up for your API keys. Go to reCAPTCHA.net and register your site and get API keys.

Step3: Client Side CAPTCHA

To display reCAPTCHA widget, you’ll need to insert snippet of code inside the <form> element where the reCAPTCHA widget will be placed:

[sourcecode language='php']require_once(‘recaptchalib.php’);
$publickey = “your_public_key”; // you got this from the signup page
echo recaptcha_get_html($publickey);[/sourcecode]
Here is the complete code
[sourcecode language='html']


< ?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>




[/sourcecode]

Don’t forget to set $publickey by replacing your_public_key with your API public key.

Step 4: Server Side Validation

The following code snippet should be placed at the top of the verify.php file, here is the code:
[sourcecode language='php']
< ?php require_once('recaptchalib.php'); $privatekey = "your_private_key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp--->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die (“The reCAPTCHA wasn’t entered correctly. Go back and try it again.” .
“(reCAPTCHA said: ” . $resp->error . “)”);
} else {
// Your code here to handle a successful verification
}
?>
[/sourcecode]

Don’t forget to set $privatekey by replacing your_private_key with your API public key.

That’s it! reCAPTCHA should now be working on your site. If you have any problem please comment it below.

recaptcha demo


Must Read

I’m a Founder of Razzil, Web 2.0 enthusiast, Developer, Coder and love the Internet. I am innovator mind and love to do creative work. Follow me on Twitter and Facebook


Must Read

2 Comments

  1. “No computer program can read distorted text” I dont think so, i saw cheaters in some online text based games are using tools like captcha decoder.

  2. Re-Captcha will help us to harden our blogs and say no to most of the spammer!

Trackbacks/Pingbacks

  1. Tweets that mention How to Use reCAPTCHA with PHP -- Topsy.com - [...] This post was mentioned on Twitter by razzil.com, razzil.com. razzil.com said: How to Use #reCAPTCHA with #PHP http://goo.gl/fb/w8Spj #tech ...

Leave a Comment

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>