CAPTCHA Helper

Bởi Nhan Duyen vào January 2016

CAPTCHA Helper

The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.

Loading this Helper

This helper is loaded using the following code:

Using the CAPTCHA helper

Once loaded you can generate a CAPTCHA like this:

  • The captcha function requires the GD image library.
  • Only the img_path and img_url are required.
  • If a word is not supplied, the function will generate a random ASCII string. You might put together your own word library that you can draw randomly from.
  • If you do not specify a path to a TRUE TYPE font, the native ugly GD font will be used.
  • The “captcha” directory must be writable
  • The expiration (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours.
  • word_length defaults to 8, pool defaults to ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’
  • font_size defaults to 16, the native GD font has a size limit. Specify a “true type” font for bigger sizes.
  • The img_id will be set as the “id” of the captcha image.
  • If any of the colors values is missing, it will be replaced by the default.

Adding a Database

In order for the captcha function to prevent someone from submitting, you will need to add the information returned fromcreate_captcha() to your database. Then, when the data from the form is submitted by the user you will need to verify that the data exists in the database and has not expired.

Here is a table prototype:

Here is an example of usage with a database. On the page where the CAPTCHA will be shown you’ll have something like this:

Then, on the page that accepts the submission you’ll have something like this:

Available Functions

The following functions are available:

create_captcha([$data = ”[, $img_path = ”[, $img_url = ”[, $font_path = ”]]]])
Parameters:
  • $data (array) – Array of data for the CAPTCHA
  • $img_path (string) – Path to create the image in
  • $img_url (string) – URL to the CAPTCHA image folder
  • $font_path (string) – Server path to font
Returns:

array(‘word’ => $word, ‘time’ => $now, ‘image’ => $img)

Return type:

array

Takes an array of information to generate the CAPTCHA as input and creates the image to your specifications, returning an array of associative data about the image.

The image is the actual image tag:

The time is the micro timestamp used as the image name without the file extension. It will be a number like this: 1139612155.3422

The word is the word that appears in the captcha image, which if not supplied to the function, will be a random string.