| 状态 | 残缺(stub) |
|---|---|
| Todo | Write me |
| 官方最后更新时间 | 2009/02/04 08:46 |
验证是用来保护您的网站上显示的内容使得除了人类可以识别而计算机无法识别。它们通常放置在注册页面,当然你也可以把它们放在任何你需要的地方来确认需要人为处理而非机器人。
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 加载)
基本和α风格绘制的图片,高度和宽度确定的大小的图片。
对于 basic 和 alpha 样式设置图片的高度和宽度。
定义产生的验证的难易程度。共有下面几种样式:
basic - [1:10] 复杂设置是用来作为字符长度计数alpha - [1:10] 复杂设置是用来作为字符长度计数word - [2:9] 复杂设置是用来作为字长math - [0;4;8] 更高的复杂性,更难的挑战
对于 basic 和 alpha 样式设置背景图片
对于 basic 和 alpha 样式的字体设置。字体是一字体文件数组。几个字体意味着随机选择。
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.
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($new_count = NULL, $invalid = FALSE) gets or sets the number of valid Captcha responses for this session. It takes:
$new_count new counter value (default NULL)$invalid trigger invalid counter (for internal use only) (default FALSE)
invalid_count($new_count = NULL) gets or sets the number of invalid Captcha responses for this session. It takes:
$new_count new counter value (default NULL)
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:
$threshold valid response count threshold (default NULL)
render($html = TRUE) returns or outputs the Captcha challenge.. It takes:
$html TRUE to output html, e.g. <img src=”#” /> (default TRUE)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();