今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子:

1、在controller中加入代码

(1)启用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
public 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
<?php
array('allow',
                'actions'=>array('captcha'),
                'users'=>array('*'),
            ),
?>

2、在model中加入代码

(1)声明

1
2
3
<?php
public $verifyCode;
?>

(2)加入属性

1
2
3
4
5
6
7
8
<?php
public function attributeLabels()
    {
        return array(
            'verifyCode'=>'Verification Code',
        );
    }
?>

(3)加入过滤规则

1
2
3
<?php
array('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的总结(转)的更多相关文章

  1. yii 验证码那点事儿

    今天要使用yii验证码, 不过, 这个验证码是整站通用的, 也就是说, 有个表单的提交是使用ajax方式来提交, 整站, 不管在哪个地方, 都能点出来此窗口, 来提交信息 关于yii验证码, fram ...

  2. yii验证码不使用model在控制器中进行验证

    控制器 public function actionCheckLogin(){ if(!$this->createAction('verify_code')->validate($_POS ...

  3. yii 验证码的使用

    在HappyController 中加入 public function actions(){ return array( // captcha action renders the CAPTCHA ...

  4. Yii验证码简单使用及

    控制器:(写了貌似也没用,未解决验证码位数:位数可改核心代码) public $layout = false;//隐藏导航 public function actions(){ return [ // ...

  5. yii 验证码功能的实现

    首先知晓我们在使用验证码的时候通常是和我们的表单小部件配合使用首先我们创建model层 新建一个php文件 名字叫做Verifycode.php 要在我们的model层 创建我们的验证码的验证规则,我 ...

  6. Yii 验证码验证

    控制器如下

  7. 关于 yii 验证码显示, 但点击不能刷新的处理

    先说说 render 与 renderPartial, 各位看官, 先别走, 我没跑题, 这个问题如果需要解决, 关键就在 render 与 renderPartial 的区别. renderPart ...

  8. Yii CActiveForm

    http://blog.sina.com.cn/s/blog_685213e70101mo4i.html 文档: http://www.yiiframework.com/doc/api/1.1/CAc ...

  9. yii 验证问题

    yii 版本2.08 yii 验证码问题 1.模型里加入'verifyCode', 'captcha','message'=>'error','captchaAction' => 'tes ...

随机推荐

  1. CF 1041 F. Ray in the tube

    F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...

  2. DELL R710使用4T硬盘亮黄灯

    事件背景 公司DELL R710的物理机上面运行的SQL SERVER数据库,因存储空间不足需要扩充空间.现系统盘(300G SAS 6Gbps 15K*2)RAID 1,数据盘(500G SAS 6 ...

  3. CobarClient源码分析

    CobarClient是阿里巴巴公司开发一个的开源的.基于iBatis和Spring的分布式数据库访问层.为了支持iBatis,Spring框架提供了一个SqlMapClientTemplate,通过 ...

  4. How to: Provide Credentials for the Dashboards Module when Using External Data Sources

    XAF中使用dashboard模块时,如果使用了sql数据源,可以使用此方法提供连接信息 https://www.devexpress.com/Support/Center/Question/Deta ...

  5. 不把DB放进容器的理由

    原文地址:http://www.tuicool.com/articles/6VbqeqQ 原文为英文,以下是笔者的个人总结. 此处的DB包括但不限于Redis.ElasticSearch. 1.数据安 ...

  6. ruby安装卸载

    1.用命令yum install ruby安装,是2.0以下的版本.不建议使用 2.2.2以上  下载地址:https://www.ruby-lang.org/en/news/2018/03/28/r ...

  7. Educational Codeforces Round 61 (Rated for Div. 2) E. Knapsack

    非常经典的dp题,因为1至8的最大公约数是840,任何一个数的和中840的倍数都是可以放在一起算的, 所以我只需要统计840*8的值(每个数字(1-8)的sum%840的总和),剩下都是840的倍数 ...

  8. PHP核心技术——接口

    接口: 接口这样描述自己:对于实现我的所有类,看起来都应该像我现在这个样子 接口含义:采用一个特定接口的所有代码都知道对于那个接口会调用什么方法. interface mobile{ public f ...

  9. Docker容器的启动与停止

    启动docker:systemctl start docker 停止docker:systemctl stop docker 重启docker:systemctl restart docker 查看d ...

  10. dstat 性能测试工具常用选项

    dstat常用的选项有: -c     显示cpu使用情况 -d     显示磁盘使用情况 -g,     显示页面数据 -i      启用中断数据 -l      平均负载统计(1分钟,5分钟,1 ...