博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii 验证码 CCaptcha的总结(转)
阅读量:7026 次
发布时间:2019-06-28

本文共 4143 字,大约阅读时间需要 13 分钟。

今天用到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参数时,设置了(相同验证码出现的次数。默认为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.οnlοad=function(){
            $('#yw0').trigger('click');
        }
        img.src = $('#yw0').attr('src');  //这段js解决yii验证码不刷新
});
</script>

 
 

转载地址:http://lioxl.baihongyu.com/

你可能感兴趣的文章
jquery淡入淡出无延迟代码
查看>>
js 规范
查看>>
OpenCV入门学习(三)HistogramEquivalent
查看>>
Intellij IDEA 10.5 语言设置
查看>>
Activity 中的Toast在Activity销毁后报错,解决方法,把context改成应用的
查看>>
解决服务器SID引起虚拟机不能加入AD域用户,无法远程登录的问题
查看>>
Don't let self-built concept imprison yourself
查看>>
08.LoT.UI 前后台通用框架分解系列之——多样的Tag选择器
查看>>
python property 学习
查看>>
perl file find
查看>>
jQuery方法position()与offset()区别
查看>>
Flume特点
查看>>
队列 句子分析 精辟的诠释 有图片
查看>>
在switch的default代码块中增加AssertionError错误
查看>>
JS:1.3,函数(function)
查看>>
Ubuntu下升级Git以及获取ssh keys的代码
查看>>
在C#代码中应用Log4Net(一)简单使用Log4Net
查看>>
webservice 测试窗体只能用于来自本地计算机的请求
查看>>
让WordPress主题支持语言本地化(使用poedit软件实现中文翻译功能)
查看>>
[WinAPI] API 6 [操作驱动器挂载点]
查看>>