关于yii验证码

  关于yii验证码的使用,一开始对这个验证码并不了解,但是正好这几天在完成个项目中要使用验证码,所以使用了他一下,具体使用方法如下

在你的控制器中配置actions方法:
  public function actions(){
            return array(
                'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
                'maxLength'=>'4',
                'minLength'=>'4',
                'width' =>'100',
                'height'=>'30',
                ),
                'page'=>array(
                'class'=>'CViewAction',
                ),
            );
        }

 

然后再你的视图模板中这么写:

<div><?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,
      'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?></div>

 

同时, 需要将captacha添加到accessRules里, 以允许所有用户访问该方法.如下

array('allow',  // allow all users to perform 'index' and 'view' actions
    'actions'=>array('index','view','captcha'),
    'users'=>array('*'),
   ),

最后:我们需要在我们的form model中添加一个verifycode的属性来存放用户输入的验证码,然后通过captcha验证器来验证用户输入的验证码的准确性。

 public $verifyCode;

 

并在rules中添加如下

 

public function rules()
 {

 

  return array(

 

...

 

array('verifyCode', 'captcha', 'on'=>'login', 'allowEmpty'=> !extension_loaded('gd')), 

 

...

 

     );

 

}

 

 

就大功告成了,但是特别注意,当你鼠标点验证码图片时候,图片并没有改变,这个时候你要注意你控制器里面的渲染方法,如果不是redner的话一定要让最后一个参数为true:

$this->renderPartial('index',array('admin_login'=>$admin_login),false,true);才能引入js

 

posted on 2014-02-07 13:39  茶壶81  阅读(237)  评论(0)    收藏  举报

导航