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] => SkaMS [image_src] => /admin/simple/simple-php-captcha.php?_CAPTCHA&t=0.39350100+1730950193 )
To display the CAPTCHA image, create an HTML <img>
using
$_SESSION['captcha']['image_src']
as the src
attribute:
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 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 )); >
session_start()
before
calling the simple_php_captcha()
function
$_SERVER['DOCUMENT_ROOT'] . '/' . [path-to-file]
)
simple_php_captcha()
in the global namespace
$_SESSION['simple-php-captcha']
session variable