How to implement reCAPTCHA
If you haven’t been spammed by bots, yet, you probably will soon be :D.
On jobber.ro, we use reCAPTCHA to prevent spam. It’s a hosted captcha provider and much more!
Step 1. Create an account with reCAPTCHA
Go ahead and create an account, download the PHP API (and copy recaptchalib.php in the _includes/ directory, and sign up for an API key.
Step 2. Add reCAPTCHA when posting an ad
config.php
Define 2 constants, one for public and one for the private API key:
define('CAPTCHA_PUBLIC_KEY', '12345_YOUR_PUBLIC_KEY');
define('CAPTCHA_PRIVATE_KEY', '12345_YOUR_PRIVATE_KEY');
Also add the following line (find the other require_once lines), which includes the recaptcha library:
require_once '_includes/recaptchalib.php';
_templates/publish-write.tpl
Right above the submit button’s fieldset, insert the following code:
<h2 class="publish_section">Anti-spam</h2>
<fieldset>
{literal}
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'white',
tabindex : 9
};
</script>
{/literal}
{$the_captcha} <span class="validation-error">{if $errors.captcha}
<img src="{$BASE_URL}img/icon-delete.png" alt="" />{/if}</span>
</fieldset>
page-write.php
On line 2, we generate the html code that displays the captcha in the template:
$smarty->assign('the_captcha', recaptcha_get_html(CAPTCHA_PUBLIC_KEY));
Next, we perform a simple validation when posting the ad. Find the following line:
if ($_POST['action'] && $_POST['action'] == 'publish')
A couple of lines below it, after you initialize the $errors array, add this code:
$resp = recaptcha_check_answer(CAPTCHA_PRIVATE_KEY,
$_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
$errors['captcha'] = 'Incorrect code';
}
Then, we need to add the captcha validation in another section of page-write.php, the one that handles the ad edit. Find the following line and add the same code as above:
else if ($_POST['action'] && $_POST['action'] == 'edit'
That should be it :).
Enjoy!
comments
7 Responses to “How to implement reCAPTCHA”
Leave a Reply
and add
require_once ‘_includes/recaptchalib.php’; in config.php
Of course, thanks!
how do we customize the look and feel of reCaptcha? By the way, today is my first time to jobber and i would like to see the requirement of this job application. Where can I find it
Some info, here: http://recaptcha.net/apidocs/captcha/client.html
You can also change it via CSS.
I followed all the steps but in:
Code:
page-write.php
On line 2, we generate the html code that displays the captcha in the template:
$smarty->assign(’the_captcha’, recaptcha_get_html(CAPTCHA_PUBLIC_KEY));
———————–
I do not know where to place this line.
Anyone helpe me…
Thanks
Hi guys,
Followed all the instructions here but I get the following error message. Help is appeciated. Thanks
Notice: Undefined index: action in /home/.kookie/page_write.php on line 3
Notice: Undefined variable: resp in /home/.kookie/page_write.php on line 9
Notice: Trying to get property of non-object in /home/.kookie/page_write.php on line 9
Notice: Undefined index: action in /home/.kookie/page_write.php on line 123
Notice: Undefined variable: resp in /home/.kookie/page_write.php on line 129
Notice: Trying to get property of non-object in /home/.kookie/page_write.php on line 129
Notice: Undefined variable: company in /home/.kookie/page_write.php on line 142
Notice: Undefined variable: title in /home/.kookie/page_write.php on line 146
Notice: Undefined variable: description in /home/.kookie/page_write.php on line 150
Notice: Undefined variable: poster_email in /home/.kookie/page_write.php on line 154
hi filip do you know how to add the captcha for the apply field because I have spam on it too… Thanks for your help.
henri