Era Captcha WorkSheet

Basic, Simple, Colorful Captcha =)

Table of Contents

Class Functions

Constructor:

__construct($array=array(),$value=null) More information about parameters, plase look set_variables function. This function's parameters are same as set_variables function.

Setting Variables

public function set_variables($array,$value=null)
// How i use this function?
$array = (
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
$captcha->set_variables( $array );
// OR
$captcha->set_variables('key1','value1');
$captcha->set_variables('key2','value2');
$captcha->set_variables('key3','value3');
This function have two variables, first one is required, and second one ise optional. If you use only one parameter at this function, you must be use an array.

Creating Captcha

public function create()
// How i use this function?
require_once 'captcha.class.php';
$captcha = new Captcha();
$captcha->create();
This function create captcha image. You must set all variables before use this function.

Validating Captcha

public function validate($captcha,$salt=NULL)
// How i use this function?
$captcha = new Captcha();
$captca->set_variable('salt','write_your_new_salt_if_you_change_it');
$captcha->validate($_POST['form_captcha']);
// OR
$captcha = new Captcha();
$captcha->validate($_POST['form_captcha'],'write_your_new_salt_if_you_change_it');
You can validate your captcha with this function. This function is decrypty your session and/or cookie data and compare with user form value. If you change your salt value at setting variables while you creating captcha image, you must specify salt value at this function. Otherwise class use your default salt value to decrypt your session and/or cookie value.

Simple Usage

Firstly create new blank file and rename it captcha.php and write this:

<?php
require_once 'captcha.class.php';
$settings = array(
'validate_type' => 'both', // session - cookie - both
'session_name' => '_era_captcha_session_',
'cookie_name' => '_era_captcha_cookie_',
'random_chars_lenght' => 10,
'security_type' => 0, // 0 - Random | 1 - Mathematic | 2 - Random Words and Numbers | 3 - Dictionary
'font_size' => 16,
'colors' => array('#D01F3C','#356AA0','#3F4C6B','#6BBA70','#73880A','#C79810','#D15600','#008C00','#FF1A00'),
'log' => false
);
$captcha = new Captcha($settings);
$captcha->create();
?>
Open your form and add like this code : ...
<label><span>Captcha Image</span> <input type="text" name="captcha" id="captcha" /> <img src="captcha.php" /></label>
...
And open your form post page and make your validation like below: <?php
$form_captcha = $_POST['captcha'];
$captcha = new Captcha();
$result = $captcha->validate($form_captcha);
if ($result) {
echo 'Captcha is valid';
} else {
echo 'Captcha is not valid';
}
?>

Extra Information

Variables

Era Captcha Class use use these variables:

Parameter
Default Value
Description
salt
Era_Captcha
use in encrypting and decrypting captcha value
validate_type
both
validating methot - you can use session, cookie or both values
session_name
_era_captcha_
session name - you must change it if you use multiple captcha image and session validating type
cookie_name
_era_captcha_
cookie name - you must change it if you use multiple captcha and cookie validating type
cookie_expire
360
it define cookie expire time in seconds. 0 (zero) expire when browser close.
cookie_path
/
if you use cookie in diffrent path etc. secure/ like, you must define this.
cookie_domain
if you use cookie in diffrent domain etc. subdomain, you must define this.
cookie_secure
false
if you use cookie with secure connection (https) you must set this value to true.
cookie_httponly
false
if you use only http connection, you can change this to true.
font_dir
./fonts
Where are the fonts? Define your fonts path.
wordlist
wordlist.php
This file must return an array and contains only Latin-1 characters.
random_chars_lenght
7
Define your random characters length.
securit_type
0
0 - Random | 1 - Mathematic | 2 - Random Characters | 3 - Dictionary
font_size
14
Font size. You can use any integer value, small value small image, big value big image
bgcolor
array('#FFFFFF', '#CDEB8B', '#C3D9FF', '#EEEEEE', '#F9F7ED')
This value must be an array and you must use HEX color codes. If you one single background color use like this: array('#FFF)
colors
array('#D01F3C', '#356AA0', '#3F4C6B', '#6BBA70', '#73880A', '#C79810', '#D15600', '#008C00', '#FF1A00')
This colors usage same like bgcolor parameter. More color, more colorful captcha =)
log
true
This class can be log everything. Errors, warning, information.
log_dir
./logs
Log file directory.
log_file
logs
Log file name. Dont write extension only file name.
log_ext
log
Define your log extension without dot.
log_daily
true
If you want to use your log file name with day-month-year define this true. If this value true log_file variable dont use.
log_level
2
0 - All (error, warning, info) | 1 - Error | 2 - Error + Warning