yii自帶驗證碼,解決刷新頁面驗證碼不刷新問題

一:打開文件 /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,//頁面刷新自動更新驗證碼
      ),
    );
  }

結束!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章