如何添加验证码,这里就不再多说了,网上有很多的资料。自己按照网上的资料搜索添加即可, 验证码添加好之后,会发现,刷新页面Yii的验证码并不会自动刷新,目前解决这个的办法有三种: 一、修改源码CCaptchaAction.php的run方法,不推荐 二、写一个js,在页面刷新的时候调用js自动点击验证码图片实现刷新,感觉有点...,太依赖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, //限制相同验证码出现的次数,0为不限制 ), ); } 现在再去刷新一下页面试试看,验证码是不是随页面刷新而刷新了呢
|