Yii内置的Captcha基本上可以满足大部分需求,如果你对验证码有特殊要求,你可以自定义Captcha,这主要是通过扩展 CCaptchaAction来实现的,本例自定义一个验证码功能,随机产生10以内的加减法,用户需要计算出正确的结果才能通过验证。
本例基于上例Yii Framework 开发教程(20) UI 组件 Captcha示例,做如下修改
首先在protected/components 目 录下创建一个MathCaptchaAction,重载generateVerifyCode, renderImage等方法:
class MathCaptchaAction extends CCaptchaAction { protected function generateVerifyCode() { return mt_rand((int)$this->minLength, (int)$this->maxLength); } public function renderImage($code) { parent::renderImage($this->getText($code)); } protected function getText($code) { $code=(int)$code; $rand=mt_rand(1,$code-1); $op=mt_rand(0,1); if($op) { return $code-$rand. '+' . $rand; }else { return $code+$rand. '-' . $rand; } } }
然后修改SiteController的rules 使用新创建的MathCaptchaAction
public function actions() { return array( 'captcha'=>array( 'class' => 'MathCaptchaAction', 'minLength' => 1, 'maxLength' => 10, )); }
本例下载:http://www.imobilebbs.com/download/yii/CustomCaptchaDemo.zip
查看全套教程:http://www.bianceng.cn/webkf/PHP/201301/35265.htm
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索return
, function
, rand
, protected
, code
, 自定义ui图片需求
, Code UI
, captcha
, yii2_rules自定义
, Yii验证码
, yii2.0验证用户名
, yii2.0验证邮箱
, yii自定义验证
yii验证规则
yii2 captcha、yii2 captcha 验证、yii captcha 验证、yii captcha、yii2 captcha 刷新,以便于您获取更多的相关知识。