yii自带验证码,解决刷新页面验证码不刷新问题

来源:互联网 发布:知乎 北上广深 编辑:程序博客网 时间:2024/05/01 15:40

一:打开文件 /framework/web/widgets/captcha/CCaptchaAction.php

添加全局变量public $toRefresh=false;

修改run方法,增加红色部分代码

    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())),
            ));
        }elseif($this->toRefresh){
              $code=$this->getVerifyCode(true);
              $this->renderImage($code);
             }

        else
            $this->renderImage($this->getVerifyCode());
        Yii::app()->end();
    }

二:控制器增加下面红色代码

  public function actions()
  {
    return array(
      //验证码
      'captcha'=>array(
        'class'=>'CCaptchaAction',
        'backColor'=>0xFFFFFF,
        'maxLength'=>4,
        'minLength'=>4,
        'width'=>80,
        'height'=>36,
        'toRefresh' => true,//页面刷新自动更新验证码
      ),
    );
  }

结束!

0 0
原创粉丝点击