reCAPTCHA with PHP
I’ve seen in many sites the reCAPTCHA plugin and I’ve particular focus to it specially since Google acquired the company.
Today I’ve tested the system in my personal site… I was getting quite a few spam messages!
The system is very simple to use and the code is pretty straightforward. I have everything on PHP so it was quite easy add the reCAPTCHA library and methods the existing contact form. Here’s the tutorial:
- Register for a key at reCAPTCHA
- Download the PHP library
- Put the following code in the form where you want the CAPTCHA to appear
<?php
require_once(‘recaptchalib.php’);
$publickey = “<your public key>”;
echo recaptcha_get_html($publickey);
?>
- Add the following code to the form validation page
<?php
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 . “)”);
}
?>
- Customize the code… remember that the most relevant part is the $resp->is_valid test. You must configure your application according to this property’s value. (If it fails, don’t submit the form; if it passes, send it).
So it was easy as this… And you also try the Mailhide, it is very useful!
To test the reCAPTCHA, just go to my site and send me a message!