yii 验证码 CCaptcha的总结(转)
今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子:
1、在controller中加入代码
(1)启用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?phppublic function actions() { return array( // 启用验证码组件 'captcha'=>array( 'class'=>'CCaptchaAction', 'backColor'=>0xFFFFFF, 'maxLength'=>4, // 最多生成几个字符 'minLength'=>4, // 最少生成几个字符 'fixedVerifyCode' => substr(md5(time()),11,4), //每次都刷新验证码 ), ); }?> |
(2)添加进入规则
|
1
2
3
4
5
6
|
<?phparray('allow', 'actions'=>array('captcha'), 'users'=>array('*'), ),?> |
2、在model中加入代码
(1)声明
|
1
2
3
|
<?phppublic $verifyCode;?> |
(2)加入属性
|
1
2
3
4
5
6
7
8
|
<?phppublic function attributeLabels() { return array( 'verifyCode'=>'Verification Code', ); }?> |
(3)加入过滤规则
|
1
2
3
|
<?phparray('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),?> |
3、在view中写代码
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php if(CCaptcha::checkRequirements()): ?> <div class="row"> <?php echo $form->labelEx($model,'verifyCode'); ?> <div> <?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?> <?php echo $form->textField($model,'verifyCode'); ?> </div> <div class="hint">Please enter the letters as they are shown in the image above. <br/>Letters are not case-sensitive.</div> <?php echo $form->error($model,'verifyCode'); ?> </div> <?php endif; ?> |
经过测试,老是报验证码错误,经过多方 查询
如果在 form中开启了,这两个
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
然后使用 'fixedVerifyCode' => substr(md5(time()),11,4),会导致这个问题发生,在这篇文章中,有详细描述为什么会出现这个问题的原因:http://blog.163.com/wangzhenbo85@126/blog/static/10136328220133921313479/
使用fixedverifycode是为了解决验证码在Yii页面刷新的时候不变的问题,但是加入这个后 开启了ajax验证,就会老是出现验证码错误的情况。经过搜索,找到了一个简单解决yii页面刷新,验证码不变的方案:
$(document).ready(function(){ var img = new Image; img.onload=function(){ $('#yw0').trigger('click'); } img.src = $('#yw0').attr('src');});经测试可以,原文地址:http://www.365joomla.com/php/yiishua-xin-ye-mian-yan-zheng-ma-bu-bian-chu-li-fang-fa
后又发现,如果form开启ajax验证,这时,如果输入错了一次验证码,再继续输入,不刷新验证码的话,就会一直出现验证码错误的情况。 一般情况是因为在设置CCaptchaAction参数时,设置了testLimit(相同验证码出现的次数。默认为3。小于等于0的值意味着不限制)为1,或则小于3,这种情况下,相同的验证码只能出现一次,而用户如果开启了ajax验证的话,填写的时候ajax验证一次已达到上限1次,提交的时候再验证一次,他会判断是否大于了testLimit的值,第二次验证testLimit会加1,显然大于了1,这时会重新生成验证码,从而出现验证码老是不正确
解决办法:
'testLimit'=>999, //这里可以设置大一些,以免验证超过三次会出错.
原文地址:http://blog.163.com/wangzhenbo85@126/blog/static/1013632822013230315743/
经过总结可以在form中开启 'enableAjaxValidation'=>true,
'enableClientValidation'=>true, 并正常使用yii 验证码,需要以下设置:
controller:
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
'maxLength'=>4, // 最多生成几个字符
'minLength'=>4, // 最少生成几个字符
'testLimit'=>999,
),);
model:
public $verifyCode; //声明verifycode存储验证码
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()), //添加此验证规则,这是是验证验证码是否一致的,不需要额外的代码就通过这条规则验 证即可
);
views:
<?php $form=$this->beginWidget('bootstrap.widgets.BsActiveForm', array(
'id'=>'login-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<div class="col-md-4"><?php echo $form->textField($model,'verifyCode',array('placeholder'=>'输入验证码')); ?></div>
<div class="col-md-6"><?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer;height:42px'))); ?></div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
<?php $this->endWidget(); ?>
<script language="javascript">
$(document).ready(function(){
var img = new Image;
img.onload=function(){
$('#yw0').trigger('click');
}
img.src = $('#yw0').attr('src'); //这段js解决yii验证码不刷新
});
</script>
yii 验证码 CCaptcha的总结(转)的更多相关文章
- yii 验证码那点事儿
今天要使用yii验证码, 不过, 这个验证码是整站通用的, 也就是说, 有个表单的提交是使用ajax方式来提交, 整站, 不管在哪个地方, 都能点出来此窗口, 来提交信息 关于yii验证码, fram ...
- yii验证码不使用model在控制器中进行验证
控制器 public function actionCheckLogin(){ if(!$this->createAction('verify_code')->validate($_POS ...
- yii 验证码的使用
在HappyController 中加入 public function actions(){ return array( // captcha action renders the CAPTCHA ...
- Yii验证码简单使用及
控制器:(写了貌似也没用,未解决验证码位数:位数可改核心代码) public $layout = false;//隐藏导航 public function actions(){ return [ // ...
- yii 验证码功能的实现
首先知晓我们在使用验证码的时候通常是和我们的表单小部件配合使用首先我们创建model层 新建一个php文件 名字叫做Verifycode.php 要在我们的model层 创建我们的验证码的验证规则,我 ...
- Yii 验证码验证
控制器如下
- 关于 yii 验证码显示, 但点击不能刷新的处理
先说说 render 与 renderPartial, 各位看官, 先别走, 我没跑题, 这个问题如果需要解决, 关键就在 render 与 renderPartial 的区别. renderPart ...
- Yii CActiveForm
http://blog.sina.com.cn/s/blog_685213e70101mo4i.html 文档: http://www.yiiframework.com/doc/api/1.1/CAc ...
- yii 验证问题
yii 版本2.08 yii 验证码问题 1.模型里加入'verifyCode', 'captcha','message'=>'error','captchaAction' => 'tes ...
随机推荐
- Oracle中用户解锁
以hr 用户为例: SQL> alter user hr account unlock; ユーザーが変更されました. SQL> alter user hr identified by hr ...
- 6 [面向对象]-property
1.特性(property) 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性 ...
- Wannafly挑战赛24 B 222333
小水题???但是时间限制异常鬼畜,跑了2min \(P | (2^m)*(3^n)-1\)的意思就是\(2^m 3^n = 1 (\text{mod }P)\) 设f[i]表示3^k=i的最小的k 然 ...
- restful_framework之APIView
一.安装djangorestframework 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在 ...
- ASP.NET MVC Bundles 之学习笔记
在网页中,我们经常需要引用大量的javascript和css文件,在加上许多javascript库都包含debug版和经过压缩的release版(比如jquery),不仅麻烦还很容易引起混乱,所以AS ...
- c# 缓存详解
如果说要对一个站点或者应用程序经常优化,可以说缓存的使用是最快也是效果最明显的方式.一般而言,我们会把一些常用的,或者需要花费大量的资源或时间而产生的数据缓存起来,使得后续的使用更加快速. 如果真要细 ...
- Rmdir方法
删除现有目录或文件夹. 语法 RmDir路径 所需的_路径_参数标识的目录或文件夹要移除的字符串表达式. _路径_可以包含驱动器. 如果没有指定驱动器, RmDir 将删除当前驱动器上的目录或文件夹. ...
- navicat连接mysql出现1251错误
刚刚安装热乎的navicat发现出现1251错误,原因不大清楚,找到一个解决办法: 将mysql的密码重新重置一遍: 1.打开命令行 ,进入mysql所在的目录,输入 mysql -uroot -p ...
- 为 GlusterFS 设计新的xlator (编译及调用过程分析)
GlusterFS 是一个开源的网络分布式文件系统,前一阵子看了一点GlusterFS(Gluster)的代码,修改了部分代码,具体是增加了一个定制的xlator,简单记录一下. Gluster与xl ...
- Linux shell中&,&&,|,||的用法
前言 在玩dvwa的命令注入漏洞的时候,遇到了没有预料到的错误,执行 ping 127.0.0.1 & echo "<?php phpinfo(); ?>" & ...