状态 残缺(stub)
Todo Write me
官方最后更新时间 2009/02/04 08:46

验证库(Captcha Library)

验证是用来保护您的网站上显示的内容使得除了人类可以识别而计算机无法识别。它们通常放置在注册页面,当然你也可以把它们放在任何你需要的地方来确认需要人为处理而非机器人。

Kohana 的验证类目前可以生成基本的字母,单词,数学算式,谜语的验证。验证配置已经在一个组中定义可让你轻松地切换不同的验证置并放在你的网站上面。

配置

配置文件存放在 application/config/captcha.php ,如果不存在请从 system/config 复制一份(不明白的请看级联系统):

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

注意

所有组继承和覆盖默认的组。

样式

风格定义的验证类型,例如基本字母,单词,数学算式,谜语共 5 种不同的样式:

  • basic - 生成随机字母的图片(仅是不同的字母数字字符)
  • alpha - 生成随机单词的图片 (仅是不同的字母字符)
  • word - 从当前的语言文件(i18n/xx_XX/captcha.php)随机加载短语询问
  • math - 通常的数学算式,比如 2 + 8 = ?
  • riddle - 询问一些谜语,比如 火是… (热的 或 冷的) (从 i18n/xx_XX/captcha.php 加载)

图片高度和宽度

基本和α风格绘制的图片,高度和宽度确定的大小的图片。 对于 basicalpha 样式设置图片的高度和宽度。

复杂度(难易程度)

定义产生的验证的难易程度。共有下面几种样式:

  • basic - [1:10] 复杂设置是用来作为字符长度计数
  • alpha - [1:10] 复杂设置是用来作为字符长度计数
  • word - [2:9] 复杂设置是用来作为字长
  • math - [0;4;8] 更高的复杂性,更难的挑战

背景

对于 basicalpha 样式设置背景图片

字体

对于 basicalpha 样式的字体设置。字体是一字体文件数组。几个字体意味着随机选择。

Promote

看代码的意思是可以通过配置 promote 这个参数来避免用户一直输入验证码,假设设置 promote 为 10 ,那么某用户已经经过 10 次验证,在当前 session 有效期内,该用户都不会再进行验证码验证,用来提高用户的体验。 by lazyboy

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:

* **[string]** ''$response'' the captcha response

valid_count()

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

  • [integer] $new_count new counter value (default NULL)
  • [boolean] $invalid trigger invalid counter (for internal use only) (default FALSE)
  • returns [integer] counter value

invalid_count()

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

  • [integer] $new_count new counter value (default NULL)

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:

  • [integer] $threshold valid response count threshold (default NULL)

render()

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

  • [boolean] $html TRUE to output html, e.g. <img src=”#” /> (default TRUE)

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();
libraries/captcha.txt · 最后更改: 2009/02/04 17:51 由 icyleaf