Adding reCAPTCHA to the FormM@iler Script
Complements of FormM@iler user, Rob Nijlaan.
For the ones who like to add real reCAPTCHA to the script we need to change some strings in the code. The form we used is split in form.php (wich displays the layout of the form) and FormMailer.php (wich verifies the input and sends the mail).
First we need to change the form;
If you open up form.php (the example enclosed with FormM@iler) and go to line 19;
<p> <?php $random = rand(0,3); ?> <img src="images/img_<?php echo $random; ?>.jpg"/><br /> <input type="hidden" name="captcha_code" value="<?php echo $random; ?>" /> Enter the characters from the image above:<br /> <input type="text" name="captcha_entry" value="" /> </p>
This displays the "sort of CAPTCHA". But for real CAPTCHA we need to replace this completely with the following code;
<p>
<?php
require_once('recaptchalib.php');
$publickey = "PUT YOUR PUBLIC KEY HERE";
echo recaptcha_get_html($publickey, $error);
?>
</p>
You maybe noticed that this code requires a key. Go to http://recaptcha.net/ and "get reCAPTCHA" and register. Once registered (Its completely free) you will get 2 key's, a Private and a Public key. Enter your Public key into the code between the " ". Now you finished the form!
The following step requires you to change the code from formmailer.php. Open formmailer.php and go to line 420.
// anti-spam CAPTCHA check
if(strlen($captcha_codes[$config]) > 0)
{
$captcha_check=preg_split('/,/',$captcha_codes[$config])
if(strtolower($_POST["captcha_entry"]) != strtolower($captcha_check[$_POST["captcha_code"]]))
$captcha_message = "<li>CAPTCHA test did not match.</li>\n";
}
This was ment to validate the "sort of CAPTCHA" but to get reCAPTCHA working we need to change this. We need to replace this code with the following;
// anti-spam CAPTCHA check
if(strlen($captcha_codes[$config]) > 0)
{
require_once('recaptchalib.php');
$privatekey = "PUT YOUR PRIVATE KEY HERE";
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
$response = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$response->is_valid)
{
// What happens when the CAPTCHA was entered incorrectly
die ($captcha_message = "<li><h2>Error: The reCAPTCHA test did not match your input, please reload the page and try again.</h2></li>\n" . "(reCAPTCHA said: " . $error = $response->error . ")");
}
}
Enter your Private key into the code between the " ". Thats it! Now we have a working reCAPTCHA! But wait... The reCAPTCHA is in English. What if we want a different language? Simple, put the following lines into your form.php. This needs to be between the <head> and </head> tag!
<script type="text/javascript" language="javascript"><!-- // --><![CDATA[
var RecaptchaOptions = { lang : 'nl' };
// ]]></script>
Maybe you noticed im using "nl" wich is the Netherlands. Replace this to get any language you want. Also there are many more customizations like theme, etc. Its possible this way to change the looks of the reCAPTCHA. So if your site has some colors, that dont go well with the reCAP theme, simply change it. For more info on how to customize reCAPTCHA please visit: http://recaptcha.net/apidocs/captcha/client.html This page contains info on language codes etc.
