Guestbook - Adding CAPTCHA

From HostBaby Wiki

Jump to: navigation, search

Guestbook SPAM is a plague unto us all.

Thankfully there is a relatively simple way for anyone who is familiar with FTP and a little bit of HTML to add CAPTCHA to their guestbook form.

CAPTCHA is a way to weed out SPAM bots by requiring it to "read" graphical text -- which spiders cannot do. This will cut back on your SPAM significantly. :)


Download the CAPTCHA files

First, go to: http://www.white-hat-web-design.co.uk/articles/php-captcha.php

Scroll to the bottom of the page and download the ZIP file that contains all the files you need.


Installing CAPTCHA

Unzip the CAPTCHA file somewhere on your local computer.

Next, FTP in to your server. If you're not familiar with FTP, please see our FTP Tutorial

Copy over 'CaptchaSecurityImages.php' and 'monofont.ttf' files from your computer into your web directory.


Add the following PHP code at the *VERY TOP* of your guestbook.html page (above the require_hostbaby line) :

<?php
session_start();
if (isset($_POST['gbcomment']) && !empty($_POST['gbcomment'])) {          
  if (isset($_SESSION['security_code']) && isset($_POST['security_code']) &&
      !empty($_SESSION['security_code']) && $_SESSION['security_code'] == $_POST['security_code']) {
    unset($_SESSION['security_code']);
  } else {
    die('
 <html>
  <head>
    <meta http-equiv="refresh" content="2;URL=guestbook.html">
  </head>
  <body>
    <h2>Sorry, you have provided an invalid security code</h2>
    You\'ll be sent back to the guestbook page in 2 seconds or click <a href="guestbook.html">here</a> now.
  </body>
 </html>');
  }
}
?>


Next, between your last text input, and your submit button, add this code:


<div align="center">
  <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" alt="security code" />
  <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" style="width:100px" />
</div>


Save, and you're done!

You should see a significant decrease in SPAM. :)

Personal tools