This is documentation for Kohana v2.3.x.

Table of Contents
Statusstub
TodoWrite me

Captcha Library

Captchas are used to protect your site by showing something that a computer can't recognize but a human can. They are usually placed on your registration page but they can be placed anywhere you want to make reasonably sure you are dealing with a person and not a bot.

Kohana's Captcha library can currently generates basic, alpha, word, math, riddle captchas. Captcha configuration is defined in groups which allows you to easily switch between different Captcha settings for different forms on your website.

Configuration

Configuration is done in the application/config/captcha.php file, if it's not there take the one from system/config and copy it to the application folder (see cascading filesystem):

$config['default'] = array
(
    'style'      => 'basic',
    'width'      => 150,
    'height'     => 50,
    'complexity' => 4,
    'background' => '',
    'fontpath'   => SYSPATH.'fonts/',
    'fonts'      => array('DejaVuSerif.ttf'),
    'promote'    => FALSE,
);

Note: all groups inherit and overwrite the default group.

Styles

style defines the captcha type, e.g. basic, alpha, word, math, riddle. There are 5 different drivers:

Picture height and width

For basic and alpha styles drawing a picture, height and width define the size of the picture.

Complexity

It defines the difficulty level of the generated captcha. Usage depends on chosen style:

Background

background is the path to background image file used for basic and alpha Captcha

Fonts

fontpath is the font file used for basic and alpha Captcha. fonts is an array of font files. Several fonts means that characters have randomized fonts choosen in the array.

Promote

promote is a valid response count threshold to promote user (FALSE to disable). This means , in a particular session, if user answers captcha correctly count times already, promote user to human, and don't annoy him any more.

Methods

valid()

valid($response) validates a Captcha response and updates response counter. It's a static method that can be used as a Validation rule also. It takes:

valid_count()

valid_count($new_count = NULL, $invalid = FALSE) gets or sets the number of valid Captcha responses for this session. It takes:

invalid_count()

invalid_count($new_count = NULL) gets or sets the number of invalid Captcha responses for this session. It takes:

reset_count()

reset_count() resets the Captcha response counters and removes the count sessions.

promoted($threshold = NULL) resets the Captcha response counters and removes the count sessions. It takes:

render()

render($html = TRUE) returns or outputs the Captcha challenge.. It takes:

Usage example

The code below demonstrates how to use captcha on a form. In your controller:

// Load Captcha library, you can supply the name of the config group you would like to use.
$captcha = new Captcha;
 
// Ban bots (that accept session cookies) after 50 invalid responses.
// Be careful not to ban real people though! Set the threshold high enough.
if ($captcha->invalid_count() > 49)
	exit('Bye! Stupid bot.');
 
// Form submitted
if ($_POST)
{
	// Captcha::valid() is a static method that can be used as a Validation rule also.
	if (Captcha::valid($this->input->post('captcha_response')))
	{
		echo '<p style="color:green">Good answer!</p>';
	}
	else
	{
		echo '<p style="color:red">Wrong answer!</p>';
	}
 
	// Validate other fields here
}
 
// Show form
echo form::open();
echo '<p>Other form fields here...</p>';
 
// Don't show Captcha anymore after the user has given enough valid
// responses. The "enough" count is set in the captcha config.
if ( ! $captcha->promoted())
{
	echo '<p>';
	echo $captcha->render(); // Shows the Captcha challenge (image/riddle/etc)
	echo '</p>';
	echo form::input('captcha_response');
}
else
{
	echo '<p>You have been promoted to human.</p>';
}
 
// Close form
echo form::submit(array('value' => 'Check'));
echo form::close();