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, 74 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!

  • Good idea about recaptcha. Thanks for code of re-captcha.
  • I think captcha is part of new pligg 1.0.3 v. Thanks
  • 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?
  • reCAPTCHA is made by those who created the CAPTCHA in the first place: Carnegie Mellon University - a very well-respected educational institution. reCAPTCHA uses a public/private key pair to protect the CAPTCHA system from being used the way you describe.
  • They say it helps decode badly scanned text from books, but couldn't it be easily linked to a bot that runs into CAPTCHAs and uses the unsuspecting, do-gooders to solve them.
  • Hey ! You can't.. Captcha's only appear when someone has reached a certain amount of comments made within a day. It's part of Myspace's SPAM filter.
  • goutamsarkar
    This code not working in pligg 9.9.5 version.
  • I have added the comment_form.tpl with the reCaptcha added to the post.
  • Good idea about recaptcha. Thanks for code of re-captcha.
  • Cool... thanks for sharing this! I find reCaptcha useful, and fascinating, at the same time. :D Will try this one out!
blog comments powered by Disqus