Adding reCaptcha to comments

Like most of you, I have had more than enough SPAM on my sites. One of the biggest sources is in the comments section. Let’s put an end to them now.

I have already shown you how to prevent spam bots from submitting stories by using the fabulous tool, reCaptcha, now let’s add it to the comment form as well.

To add it to your comment form, just do the following steps:

Download the latest reCaptcha library from here and upload to your “root”.

You will also need reCaptcha keys for your site, you will need to register and add your site to reCaptcha. Register for reCaptcha keys here.

Open comment_form.tpl, find:

{if $Spell_Checker eq 1}
<input class="log2" onclick="openSpellChecker(‘comment’);" name="spelling" type="button" value="{#PLIGG_Visual_Check_Spelling#}" />{/if}

After, add:

{php}

require_once(‘recaptchalib.php’);
$publickey = "ENTER PUBLIC KEY HERE";
$privatekey = "ENTER PRIVATE KEY HERE";

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;

# are we submitting the page?
if ($_POST["submit"]) {
  $resp = recaptcha_check_answer ($privatekey,
                                  $_SERVER["REMOTE_ADDR"],
                                  $_POST["recaptcha_challenge_field"],
                                  $_POST["recaptcha_response_field"]);

  if ($resp->is_valid) {
    echo "You got it!";
    # in a real application, you should send an email, create an account, etc
  } else {
    # set the error code so that we can display it. You could also use
    # die ("reCAPTCHA failed"), but using the error message is
    # more user friendly
    $error = $resp->error;
  }
}
echo recaptcha_get_html($publickey, $error);
{/php}

save. Thats it.

Here is the updated comment_form.tpl file with the reCaptcha code added. –

  Comment Form (1.7 KiB, 180 hits)

This will eliminate all comments being left by spam bots and will also make human spamming less atractive as well.

If you use this module, leave me a comment below or perhaps think of linking back to http://www.pliggs.com

Be sure to follow me on Twitter as well for one on one help and news.

If you enjoyed this post, make sure you subscribe to my RSS feed!

  • Thanks for sharing this valik! I have been searching for a solution such as this. Cheers!
  • ReCaptcha really helps in making spamming less. This is a new strong way.
  • you haven't applied this in your blog. may i ask why? interesting past by the way :)
  • It's great to see you taking action by applying this recaptcha code to blogs. ;)

  • recaptcha are my favorites for every app. Otherwise we keep receiving spams all the time.
  • How do I add reCAPTCHA to an html form and how do you make it redirect to another page if the CAPTCHA code is correct?
  • If I can't add it to an HTML form, how can I make a PHP form with JUST the reCAPTCHA and a submit button that redirects to another page.
  • It's great to see you taking action by applying this recaptcha code to blogs. Though at the moment, I don't see it currently live on your blog? So I am going to assume it is for us the readers to use :)

    Till then,

    Jean
  • For some reason my myspace account requires people to solve Captcha
  • thanks a lot!
blog comments powered by Disqus