Quickstart Guide

Securimage requires no configuration after downloading. All you need to do is display the captcha image somewhere in your form, and validate the code submitted from within your form processor. Customising the image is also very easy. This guide explains the process of adding Securimage to an existing form.

First, download Securimage and upload the files to your web server if you have not done so already. In this example, we assume that the Securimage files have been uploaded to a folder named “securimage” in the root of your web directory (i.e. www.yoursite.com/securimage/).

Next, we will insert the captcha image into your form along with a text field for the user to type the code in.

At the desired position in your form, add the following code to display the CAPTCHA image:

<img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />

Next, add the following HTML code to create a text input box:

<input type="text" name="captcha_code" size="10" maxlength="6" />
<a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a>

Note: You can change the maxlength and size properties to match your image settings.
The second line is optional but gives the user the ability to display a new image if they are having trouble reading the image that was displayed.

Once you get the image added to your form and are satisfied with the look, we will move onto editing the PHP code that validates the CAPTCHA code typed in by the user.

Open the PHP file that processes the form data after submission.
You can find this by looking at the action value inside your <form> tag.

Note: In order to use Securimage, your form processor must be written in PHP.

On the very first line of the form processor, add the following code:

<?php session_start(); ?>
It is important to put this at the top of the file before any HTML output, otherwise the validation may always fail.

The next few steps will vary depending on how form validation is handled in your code. If you have little or no php knowledge the next part can be difficult.

To check if the code is correct, we will make a call to the Securimage class. The following php code should be integrated into the script that processes your form and should be placed where error checking is done. It is recommended to place it after any error checking and only attempt to validate the captha code if no other form errors occured. It should also be within <?php ?> tags.

include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';

$securimage = new Securimage();

This includes the file that contains the Securimage source code and creates a new Securimage object that is responsible for creating, managing and validating captcha codes.

Next we will check to see if the code typed by the user was entered correctly.

if ($securimage->check($_POST['captcha_code']) == false) {
  // the code was incorrect
  // you should handle the error so that the form processor doesn't continue

  // or you can use the following code if there is no validation or you do not know how
  echo "The security code entered was incorrect.<br /><br />";
  echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";

The call to the check method checks the generated CAPTCHA code against the code entered by the user. If the code was incorrect, an error message is printed with a link to go back to the form and the script is terminated with exit().
Make sure you check the code BEFORE the form is emailed or entered into a database and only if there were no other form errors.

Following the directions above should get Securimage working with minimal effort. Learn how to customize the captcha images or check out the faq page if you are having problems getting your form to work.

Leave a Reply

Play CAPTCHA Audio
Reload Image