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("captcha.php");
$_SESSION['captcha'] = captcha();

?>

Dump of $_SESSION['captcha']:

Array
(
    [code] => FnEQv
    [image_src] => /simple-php-captcha.php?_CAPTCHA&t=0.43508000+1585544694
)

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

CAPTCHA

To verify the CAPTCHA value, just test against the $_SESSION['captcha']['code']. 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

// Showing default values
$_SESSION['captcha'] = captcha( array(
	'min_length' => 5,
	'max_length' => 5,
	'png_backgrounds' => array('default.png', ...),
	'fonts' => array('times_new_yorker.ttf', ...),
	'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
	'min_font_size' => 24,
	'max_font_size' => 30,
	'color' => '#000',
	'angle_min' => 0,
	'angle_max' => 15,
	'shadow' => true,
	'shadow_color' => '#CCC',
	'shadow_offset_x' => -2,
	'shadow_offset_y' => 2
));

>

Notes