fcOnTheWeb Logo Web technologies, made easy.

fcOnTheWeb Red DotImplementing reCAPTCHA

Today we take a look at CAPTCHA, or more specifically reCAPTCHA who provide an easy to use and control version of CAPTCHA.

A CAPTCHA is the funny looking text that is displayed on a web site to make you prove you are human. It works by showing distorted text to you and having you type in the characters. In theory the text is distorted enough so that it cannot be deciphered by a computer, but it can be deciphered by you.

A CAPTCHA is used as a spam prevention tool in a number of cases:

as well as many other cases. In each instance the aim is to prevent malicious computer scripts from abusing the system.

So we will take a look at reCAPTCHA's version and how you can easily implement it on your site. We are going to look specifically at the PHP version (as fcOnTheWeb is run on PHP) but they offer many other plugins and versions for a variety of setups.

You can see our working version of this technology here: http://fcOnTheWeb.com/articles/captcha/recaptcha.php.

Firstly you will need to go to reCAPTCHA and register with the site. This is necessary as you will be allocated two keys which you will need to validate the submission to your domain. In other words, to ensure any submission to your destination page comes from the appropriate launch page.

Once you are registered, the implementation is as easy as following the steps in the "reCAPTCHA Quickstart" section: http://recaptcha.net/plugins/php/.

Start by downloading the latest library of files from Google Code: http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest. Once you have unpacked the ZIP file you will need to put the files onto your web server. Strictly you will only need the recaptchalib.php but you can upload the other files if you feel like it.

Now you are ready to put the first part of the code on your page. This part will present the CAPTCHA to your user. The code looks like the following:

require_once("recaptchalib.php");

$publickey = "..."; // you got this from the signup page

echo recaptcha_get_html($publickey);

You will need to make sure the require_once path points to the location of your recaptchalib.php, and you will need to put your public key into the appropriate place above.

The next step is to put this code into your form with a submit button and then we will address the page the form submits to.

There is one simple block of code we will add to the submission page. The code is below:

require_once("recaptchalib.php");

$privatekey = "...";

resp = recaptcha_check_answer ($privatekey,

$_SERVER["REMOTE_ADDR"],

$_POST["recaptcha_challenge_field"],

$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {

die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .

"(reCAPTCHA said: " . $resp->error . ")");

}

Again you will need to ensure your path to recaptchalib.php is correct and you will need to enter your private key into the code.

And that is about all there is to it. You will need to add your appropriate logic and code in the correct places. For example you can add your failure code in the if statement above, and perhaps add an else section that will handle your form processing.

You can see our working version of here: http://fcOnTheWeb.com/articles/captcha/recaptcha.php.

Shortly we will be implementing this reCAPTCHA into our comment section as part of out commenting experiments (part one being nofollow and dofollow links) and we will examine its effects.

If you have any questions about implementing reCAPTCHA or about this article, leave a comment or contact us and we will look into it for you.

ferrari_chris

Human Spambot says:

2010-09-10 00:59:45

As you can see from my 'name', CAPTCHA and reCAPTCHA can also block humans from submitting comments and other things, especially dyslexic humans. That's why I call them Submission Prevention Text. 1 attempt.

__________

Jonathan Andrews says:

2010-10-15 15:22:32

interesting article.

thanks alot


Add your comment on this article below:

Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.

Form submitting now...

Name:

Website:

Email address (not displayed):

Enter your comment below: