In A Row

Get your ducks there!

home :: development :: recaptcha

:: reCAPTCHA -- Fight Spam, Read Books

In a startling development (to me), my small blog started getting spammed like crazy. Out of 100+ posts, a handful were legitimate. After going through the process of manually deleting them all, I decided to invest a bit of time in a CAPTCHA system for my blosxom blogging software. Having seen the reCAPTCHA interface on several sites, and read about it before, I decided to start there.

Basically, reCAPTCHA provides some incredibly simple APIs to add a CAPTCHA to your site. The neat part about it, though, is that the images presented to the user are actually difficult to scan words from real-life books. The results of people submitting these CAPTCHAs are combined to improve the digitization of hard-copy archives. The learn more page on the reCAPTCHA site has a more detailed explanation.

Implementation

Step 1 - Sign Up, Get Keys

Here

Step 2 - Add the the interface to the front end

	<script	type="text/javascript"
		src="http://api.recaptcha.net/challenge?k=$MYKEY">
	</script>
 
	<noscript>
		<iframe src="http://api.recaptcha.net/noscript?k=$MYKEY"
			height="300" width="500" frameborder="0"></iframe><br>
		<textarea name="recaptcha_challenge_field" rows="3" cols="40">
		</textarea>
		<input type="hidden" name="recaptcha_response_field" 
		value="manual_challenge">
	</noscript>

This javascript (and iframe) API takes several parameters, allowing you to customize the look and feel of the interface.

Step 3 - Validate the submitted reCAPTCHA

As bloxsom is written in perl, I went with the CPAN module Captcha::reCAPTCHA.

	use Captcha::reCAPTCHA;
	my $rec = Captcha::reCAPTCHA->new;
  
	my $rec_res = $rec->check_answer('6LdWHQEAAAAAAORoD7pXt_BAHRdsZ2GPjmcFdWWi', $ENV{'REMOTE_ADDR'},
			param('recaptcha_challenge_field'), param('recaptcha_response_field'));
		
	// [snip some lines]
 
	if (!$good || !$rec_res->{is_valid}) {
		$comment_response = "Some required fields were missing.";
		if (!$rec_res->{is_valid}) { 
			$comment_response = 'Please verify that you are human, with the reCAPTCHA!'; 
		}
	}

Obviously, not the most elegant code, but it was an incredibly simple process... and my blog has been spam free for 3 weeks!


Show Related Articles

Comments

Add a Comment:
 
Name:
URL/Email: [http://... or mailto:you@wherever] (optional)
Comment:
Please verify that you are human, or a good AI/OCR combo:
  Add to Technorati Favorites   Copyright 2008, Chris Moyer