CAPTCHA Image Verification Class

What is CAPTCHA?

CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is the practice of employing a test to determine whether or not responses are coming from a computer or a human. In this case, we use an image with random characters for users to read, and enter into the form. It is not generally easy for a computer to read an image in such a fashion.

Why Use It?

The reason to use an image, whose characters are obscured, is so bots can't easily analyze the image binary data and extrapolate the proper characters. This is a great practice when you don't want to require people to make an account on your site, yet you want to keep out spam bots.

Documentation

I've just used phpDoc to expand my class comments into something more user-friendly. Have a look at my new CAPTCHA docs.

Using and Expanding the Class

Using the class is very simple, and easy to integrate into any type of existing framework. Expanding the class is also relatively easy. Depending on your skills with the GD library, you can add as much features or visual effects as desired. The image resource is a public property, and is thus accessible to GD functions used on-the-fly, or other functions/classes you might be using.

This class is especially helpful for people who intentionally want something light-weight and small, and then only add-on, if necessary.

Fonts and Colors

A few fonts and their bold and italic variants have been given familiar constant names so that you don't have to type out the actual file of the font you're using. You can check which fonts are supported by checking the CAPTCHA class source code.

Several color names can also be passed as a string to the setColor method in the CAPTCHA class. For example, darkred, gray, lightgray, orange. There are still several more, and it's simple to add more, if needed. Otherwise, you can easily pass a string with the red, green, and blue decimal values, where each value is comma-separated.

Checking submitted Keys

Validating the key is also integrated into the class. By calling the validate method, you can pass the POST or GET variable followed by the option for case-sensitivity. Set TRUE for case-sensitive checking, and FALSE (default) otherwise. The class stores the randomly generated key for the image in the session variable: $_SESSION['captcha_token'].

Source

The class utilizes PHP5 OOP, but I may offer a PHP4 compatible version later.

Select and copy the highlighed source code after clicking the link. Then paste it into a text editor, such as notepad, SCiTE, VIM, etc. (not MS word). Save the file with a .php extension. In Notepad, you may have to surround the whole file name in quotes when saving, if your system is not acquainted with the .php extension.

  1. CAPTCHA class source code
  2. Source for this page
  3. Example image used here

Example

Unfortunately, my host has yet to compile PHP with GD support, thus the example is temporarily down. Hopefully, we'll have the example up and running, soon. However, the source is still available for you to use.

GD is not available