Adding a CAPTCHA to Pixie CMS blog and articles

After getting a stack of spam to my posts here, I decided to implement a CAPTCHA system to try and combat it.
This seems to be working well so I will post how to implement it in your own Pixie CMS site.

The first thing you will need is a recapture account, which is completely free and gives you a public and private key you need to communicate to their servers.
Go to http://recaptcha.net/ and sign up.
Next step you will need to download the reCAPTCHA Library from their site, unzip this and upload it to a suitable location on your site.
Now we are into modifiying a core Pixie file, namely: dyanmic.php which is located in: admin/modules/  so fire this up in your favourite editor.

The first step we will take is adding the check from the form post.
You want to be looking around line 77 after the statement: $scream = array();

Now insert the following lines:

// Check comment ok
    require_once('recaptchalib.php');
// Get a key from http://recaptcha.net/api/getkey
    $publickey = "YOURKEYFROMRECAPTCHASITE";
    $privatekey = "YOURKEYFROMRECAPTCHASITE";
    # the response from reCAPTCHA
    $resp = null;
    # the error code from reCAPTCHA, if any
    $error = null;

# was there a reCAPTCHA response?
    if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);

  if ($resp->is_valid) {
    $captcha_ok = true;
  } else {
    $captcha_ok = null;
  }
}

            if(!$captcha_ok) { $error .= "Captcha incorrect"; } // Implement this from language file to do it properly :)

Your next line should read: if (!$name) { $error .= $lang['comment_name_error']." |"; $scream[] = "name"; }

Right, next up is displaying the CAPTCHA code itself. This is done much further down the code at around line 540 (taking into consideration the above mod).
First line should be:   $posty = createURL($s, $m, $x);
Now insert the following line: $captcha_ok = null;
That should be before the echo " which starts the form display.

Now we need to display the captcha code.
This involves splitting the echo for the form which starts at around line 587 with the split occuring at around line line 602 which originally will look like this:
<div class=\"form_row_submit\">
You will need to change this to: <div class=\"form_row_submit\">";

Now insert the following:
// Captcha display   
    require_once('recaptchalib.php');
// Get a key from http://recaptcha.net/api/getkey
    $publickey = "YOURKEYFROMRECAPTCHASITE";
    echo recaptcha_get_html($publickey, $error);

                                    echo "

Note the last echo command there which continues with the form output.

That's it. You should now have a CAPTCHA system for your Pixie CMS working for the comments field :)

By Simon Britton Last updated: 4 July 2009, 08:23

Comments

No comments yet...
Leave a comment

Comment form is Gravatar and coComment enabled.

Recent Posts: