当前位置:首页 > 芯闻号 > 充电吧
[导读]原因:每次刷新页面的时候都会调用CCaptcha这个widget的run方法来运行这个助手:/** * Renders the widget. */public function run(){    

原因:
每次刷新页面的时候都会调用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



对应修改生成验证码的controller如下:

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

摘 要:“Apache+php+ MySQL”组成了一套完整的开发B/S架构的网络信息系统的工具。文中以该套工具开发产品售后服务管理系统为例,介绍了开发过程中的技术难点及解决方法。

关键字: Apache php MySQL 产品售后服务管理系统

PHP 7.4.9 版本现已发布,具体更新内容如下:Apache:修复了错误#79030(升级 apache2handler 的 php_apache_sapi_get_request_time 以返

关键字: php

如果使用美国服务器创建网站,则必须在美国服务器系统上创建环境。 今天,我将介绍美国服务器Linux系统的工作方式。

关键字: apache Linux php

近日消息,PHP 8.0将于11月发布,但当这个重要的新版本出现时,它遇到了很大的挫折,Windows将不支持它,原因未知。

关键字: php Windows 微软

2020 年 6 月 8 日,PHP 迎来了自己的 25 周岁生日。JetBrains 在博客中梳理了该语言自 1995 年诞生以来的种种历程,这种语言最初是用 C 语言编写的一组通用网关接口(C

关键字: php

function logging() { var x = new XMLHttpRequest(); x.onre

关键字: php

C++需要实现PHP端的:bin2Hex函数,PHP通过这种类型的字符串调用:pack转换成PHP能识别的2进制数据。 C++需要做的是实现一个bin2hex,其实只是把c++读取的2进制数据当成b

关键字: C语言 php

方法一: 在 php 端 header('HTTP/1.1 204 No Content '); 利用http的原理进行 方法二:利用src图片加载的特性完成请求 写一个函数,函数体内 var i

关键字: php

php与nginx整合 PHP-FPM也是一个第三方的FastCGI进程管理器,它是作为PHP的一个补丁来开发的,在安装的时候也需要和PHP源码一起编译,也就是说PHP-FPM被编译到PHP内核中,因

关键字: nginx php

Web 中文字体应用指南 在 Web 上应用字体是一项基本技术,同时也是一门艺术。对于英文字体来说可选择的范围实在是太广泛了,合理的使用它们将会为你的网站增色不少。关于英文字体的使用和搭配技巧,在这

关键字: php学习记录 yii
关闭
关闭