CAPTCHA Example

Usage

The following code will prepare a CAPTCHA image and keep the code in a session variable for later use:

<?php
session_start();
include("simple-php-captcha.php");
$_SESSION['captcha'] = simple_php_captcha();
?>

After the call to simple_php_captcha() above, $_SESSION['captcha'] will be something like this:

Array
(
    [code] => u9XeY
    [image_src] => /en/php/simple-php-captcha/simple-php-captcha.php?_CAPTCHA&t=0.10060000+1734204992
)

To display the CAPTCHA image, create an HTML <img> using $_SESSION['captcha']['image_src'] as the src attribute:

CAPTCHA code

To verify the CAPTCHA value on the next page load (or in an AJAX request), test against $_SESSION['captcha']['code']. You can use strtolower() or strtoupper() to perform a case-insensitive match.

Configuration

Configuration is easy and all values are optional. To specify one or more options, do this:

<?php

$_SESSION['captcha'] = simple_php_captcha( array(
	'min_length' => 5,
	'max_length' => 5,
	'backgrounds' => array(image.png', ...),
	'fonts' => array('font.ttf', ...),
	'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789',
	'min_font_size' => 28,
	'max_font_size' => 28,
	'color' => '#666',
	'angle_min' => 0,
	'angle_max' => 10,
	'shadow' => true,
	'shadow_color' => '#fff',
	'shadow_offset_x' => -1,
	'shadow_offset_y' => 1
));

>

Notes