reCaptcha
February 6 2009Putting together a decent captcha solution by-hand can be quite a bit of work. So why not simplify the whole process and re-use an existing solution like reCaptcha? They do a really good job of managing this service for you (and it's totally free).
Step 1: Download
Download the reCaptcha library (reCaptcha calls them plugins) from the reCaptcha website.
Note: I used the PHP library
Step 2: Install
Place the reCaptcha library some place on your webserver where you can include it into your PHP form.
Step 3: Register
You can't use reCaptcha without a registered API key so you'll have to register for one. Don't worry, it's free.
Step 4: Code
Build a PHP form that includes the reCaptcha library so that you can test out your account to make sure it's working. You can use the following PHP code:
standalone.php<?php
include('recaptchalib.php');
$challenge = (isset($_POST["recaptcha_challenge_field"]) ? $_POST["recaptcha_challenge_field"] : null);
$response = (isset($_POST["recaptcha_response_field"]) ? $_POST["recaptcha_response_field"] : null);
$publickey = 'your_public_key';
$privatekey = 'your_private_key';
$result = '';
if (($challenge != null) && ($response != null)){
$test = recaptcha_check_answer($privatekey, $_SERVER['REMOTE_ADDR'], $challenge, $response);
if ($test->is_valid) $result = 'Congratulations! You appear to be human ... <input type="submit" value="try again"/>';
else $result = recaptcha_get_html($publickey, $test->error) . '<br/><input type="submit" value="test"/>';
} else {
$result = recaptcha_get_html($publickey) . '<br/><input type="submit" value="test"/>';
}
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<?php echo $result; ?>
</form>
Step 5: Test
Load up your new PHP form and try it out. It should work like this one.