A small but vocal amount of users have asked for CAPTCHA support in simpleContact Pro.
I must say, I am not a fan of CAPTCHAs. I consider them to be inadequate from a usability and accessibility point of view and decent server-side validation can stop most spam. I use my own applications on my websites and receive practically no spam. I'm not too inflexible though to deny user's requests if they can be catered for by a straightforward hack, so here is my solution to CAPTCHA support.
Please note: As I indicated in my definition of a hack, this post doesn't indicate my intention to build a CAPTCHA into a general release.
------------------------------------------
reCAPTCHA is one of the better CAPTCHAs out there. It provides an audio alternative and also provides a useful service by helping to digitise books. I will use reCAPTCHA for this hack.
First, you must visit http://recaptcha.net and sign up for a free account. You can choose your own username and password and enter the domain (or domains) that you want to use reCAPTCHA on. You will be provided with a private key and a public key. You will need both of these to implement this hack.
1) Download reCAPTCHA PHP library
Download the reCAPTCHA PHP library and upload it to your sc_admin/include folder.
2) Modify contact form validation
Open sc_admin/include/contact.php
Add the following code from line 9:
include_once($include_directory."/recaptchalib.php");
$publickey = "YOUR-PUBLIC-KEY-HERE";
$privatekey = "YOUR-PRIVATE-KEY-HERE";
Add the following code from line 143 (on one line):
$resp = recaptcha_check_answer($privatekey,$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
Add this code onto the next line:
if (!$resp->is_valid) { $errors["recaptcha"] = "reCAPTCHA entered incorrectly"; }
3) Modify contact form
Open contact.php
Find the line that says:
<?php showForm(); ?>
Add this code from the next line:
<?php
print recaptcha_get_html($publickey);
if (array_key_exists("recaptcha",$errors)) {
printf("<p class=\"err\"><em>%s</em></p>",$errors["recaptcha"]);
}
?>
If you view your contact form you should now have a working reCAPTCHA before the submit button.