How to Use reCAPTCHA with PHP
TweetreCAPTCHA 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.
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']
[/sourcecode]
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]
That’s it! reCAPTCHA should now be working on your site. If you have any problem please comment it below.
Must Read
2 Comments
Trackbacks/Pingbacks
- 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 ...















Raza Rahil Hussain (born February 7, 1990 in Udaipur, India) is Blogger, Developer, and CEH. He founded the blog Razzil, a site that describe information about Technology, Programming, Gadgets, Apps, Internet Security and various other content.
“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.
Re-Captcha will help us to harden our blogs and say no to most of the spammer!