yii刷新页面验证码不变修改 装载自山的那边很漂亮

原因:
每次刷新页面的时候都会调用 CCaptcha 这个widget的run方法来运行这个助手:
/**
 * Renders the widget.
 */
public function run()
{
    if(self::checkRequirements())
    {
        $this->renderImage();   //生成验证码图片
        $this->registerClientScript();
    }
    else
        throw new CException(Yii::t('yii','GD and FreeType PHP extensions are required.'));
}
 
/**
 * Renders the CAPTCHA image.
 */
protected function renderImage()
{
    if(!isset($this->imageOptions['id']))
        $this->imageOptions['id']=$this->getId();

   //生成验证码图片链接src地址,这个是生成图片的关键,指向action为 $captchaAction='captcha'的方法,即调
用CCaptchaAction这个方法来生成验证码图片
    $url=$this->getController()->createUrl($this->captchaAction,array('v'=>uniqid()));
    $alt=isset($this->imageOptions['alt'])?$this->imageOptions['alt']:'';
    echo CHtml::image($url,$alt,$this->imageOptions);
} 

CCaptchaAction中的执行流程:

/**
 * Runs the action.
 */
public function run()
{
    if(isset($_GET[self::REFRESH_GET_VAR]))  // AJAX request for regenerating code
    {
        $code=$this->getVerifyCode(true);
        echo CJSON::encode(array(
            'hash1'=>$this->generateValidationHash($code),
            'hash2'=>$this->generateValidationHash(strtolower($code)),
            // we add a random 'v' parameter so that FireFox can refresh the image
            // when src attribute of image tag is changed
            'url'=>$this->getController()->createUrl($this->getId(),array('v' => uniqid())),
        ));
    }
    else
        $this->renderImage($this->getVerifyCode());  //刷新页面时会调用这个,问题就出现在这,他调用
这个方法的时候没有传递参数true
    Yii::app()->end();
} 

/**
 * Gets the verification code.
 * @param boolean $regenerate whether the verification code should be regenerated.
 * @return string the verification code.
 */
public function getVerifyCode($regenerate=false) //从这个参数可以看出 如果$regenerate为true,则会
重新生成验证码图片
{
    if($this->fixedVerifyCode !== null) 
        return $this->fixedVerifyCode;
 
    $session = Yii::app()->session;
    $session->open();
    $name = $this->getSessionKey();
    if($session[$name] === null || $regenerate)
    {
        $session[$name] = $this->generateVerifyCode();
        $session[$name . 'count'] = 1;
    }
    return $session[$name];
} 


解决办法:

一:根据getVerifyCode这个方法中的这段代码来修改,这段代码是用于验证的,如果设定了fixedVerifyCode,则每次
生成时都会生成一个固定的验证码,我们所要做的是把这个固定的变成动态的。  
 
if($this->fixedVerifyCode !== null) 
        return $this->fixedVerifyCode;

修改控制器中生成验证码的配置:
/**
 * Declares class-based actions.
 */
public function actions()
{
    return array(
        // captcha action renders the CAPTCHA image displayed on the register page
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'fixedVerifyCode' => substr(md5(time()),0,4), 
            'foreColor' => 0x55FF00,
            'testLimit' => 0,  //不限制相同验证码出现的次数
            'offset' => 5,
            'minLength' => 4,
            'maxLength' => 4,
            'transparent' => true,
        ),
    );
} 


二、继承CCaptchaAction这个类,修改 run()方法中的 $this->renderImage($this->getVerifyCode())这句为
$this->renderImage($this->getVerifyCode(true)),其他不变

缺点:这种方法在CActiveForm开启enableClientValidation=true时,总是报验证码不正确,
enableAjaxValidation开启没事,待解决。。。

代码如下:
继承的类DCCaptchaAction.php

yii刷新页面验证码不变修改 - 一束光 - 山的那边很漂亮

对应修改生成验证码的controller如下:
yii刷新页面验证码不变修改 - 一束光 - 山的那边很漂亮
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值