如何添加驗證碼,這里就不再多說了,網(wǎng)上有很多的資料。自己按照網(wǎng)上的資料搜索添加即可, 驗證碼添加好之后,會發(fā)現(xiàn),刷新頁面Yii的驗證碼并不會自動刷新,目前解決這個的辦法有三種: 一、修改源碼CCaptchaAction.php的run方法,不推薦 二、寫一個js,在頁面刷新的時候調(diào)用js自動點擊驗證碼圖片實現(xiàn)刷新,感覺有點...,太依賴js了不太好吧
三、在components文件夾下新建一個文件Captcha.php 添加如下代碼,重寫run方法: class Captcha extends CCaptchaAction{ //重寫run方法,使得驗證碼在頁面刷新時刷新 public function run(){ if (isset($_GET[self::REFRESH_GET_VAR])){ $code = $this->getVerifyCode(true); echo CJSON::encode(array( 'hash1' => $this->generateValidationHash($code), 'hash2' => $this->generateValidationHash(strtolower($code)), 'url' => $this->getController()->createUrl($this->getId(), array('v' => uniqid())), )); }else { $this->renderImage($this->getVerifyCode(true)); Yii::app()->end(); } } } 之后修改controller中class為captcha即可,代碼如下 public function actions(){ return array( 'captcha'=>array( 'class'=>'Captcha', 'backColor'=>0xFFFFFF, 'maxLength'=>'4', // 最多生成幾個字符 'minLength'=>'4', // 最少生成幾個字符 'height'=>'40', 'width'=>'230', 'transparent'=>true, //顯示為透明 'testLimit' => 0, //限制相同驗證碼出現(xiàn)的次數(shù),0為不限制 ), ); } 現(xiàn)在再去刷新一下頁面試試看,驗證碼是不是隨頁面刷新而刷新了呢
|