如何添加驗(yàn)證碼,這里就不再多說(shuō)了,網(wǎng)上有很多的資料。自己按照網(wǎng)上的資料搜索添加即可, 驗(yàn)證碼添加好之后,會(huì)發(fā)現(xiàn),刷新頁(yè)面Yii的驗(yàn)證碼并不會(huì)自動(dòng)刷新,目前解決這個(gè)的辦法有三種: 一、修改源碼CCaptchaAction.php的run方法,不推薦 二、寫(xiě)一個(gè)js,在頁(yè)面刷新的時(shí)候調(diào)用js自動(dòng)點(diǎn)擊驗(yàn)證碼圖片實(shí)現(xiàn)刷新,感覺(jué)有點(diǎn)...,太依賴(lài)js了不太好吧
三、在components文件夾下新建一個(gè)文件Captcha.php 添加如下代碼,重寫(xiě)run方法: class Captcha extends CCaptchaAction{ //重寫(xiě)run方法,使得驗(yàn)證碼在頁(yè)面刷新時(shí)刷新 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', // 最多生成幾個(gè)字符 'minLength'=>'4', // 最少生成幾個(gè)字符 'height'=>'40', 'width'=>'230', 'transparent'=>true, //顯示為透明 'testLimit' => 0, //限制相同驗(yàn)證碼出現(xiàn)的次數(shù),0為不限制 ), ); } 現(xiàn)在再去刷新一下頁(yè)面試試看,驗(yàn)證碼是不是隨頁(yè)面刷新而刷新了呢
|