jobberBase dev blog

jobberBase is the open-source job board software
that helps you set up a jobsite in minutes!

Go ahead and Download jobberBase »

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!

Category: How-to

Tagged:

15 Responses

  1. samuele says:

    and add
    require_once ‘_includes/recaptchalib.php’; in config.php ;)

  2. Filip says:

    Of course, thanks! :)

  3. amin says:

    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

  4. Filip says:

    Some info, here: http://recaptcha.net/apidocs/captcha/client.html
    You can also change it via CSS.

  5. Wagner says:

    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

  6. Eugene says:

    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

  7. henri says:

    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

  8. nirav says:

    you should sort out the apply for jobs section and implement the antispam thing asap man!!! this is getting too much messed up..

    nirav

  9. Joris says:

    I did all things described, but I don’t see a captha…

  10. Joris says:

    A small change to fit better with the rest:
    ———-
    Delete:
    Anti-spam
    ———-
    add after :
    Anti-spam

  11. Mike says:

    Beautiful implementation and instructions, worked the first time!

  12. Hello there,

    Thanks for the post.

    Is it very simple to implement the re-CAPTCHA for the APPLY Page as well?

    Thanks for HELP.

  13. chris says:

    Dear Filip,

    Thanx for this great explanation. But I’ve got the same question as Marc-Oliver. What do I have to do to make it apply to the APPLY page. What are the crucial steps you have to follow?

  14. Estevao says:

    If i add the line require_once ‘_includes/recaptchalib.php’; in config.php my first page shows totally blank!!!

    any thoughts on this?

Leave a Reply





Always happy to hear from you!

Follow us on twitter @jobberbase
or email us at hello [at] jobberbase.com