在Yii中使用Captcha验证码

来源:互联网 发布:mac系统恢复被删除 编辑:程序博客网 时间:2024/05/23 02:04
在Yii中使用Captcha验证码:

详细代码请参考:yii自带的示例代码post 项目,里面有一个contact表单用到了验证码.

1,Model:

将验证码加入UserLogin的一个属性:

[php] view plaincopy
  1. class UserLogin extends CFormModel  
  2. {  
  3.     public $username;  
  4.     public $password;  
  5.     public $rememberMe;  
  6.     public $verifyCode;  
  7.   
  8.     public function rules()  
  9.     {  
  10.         return array(  
  11.             // username and password are required  
  12.             array('username, password,verifyCode''required'),  
  13.             // rememberMe needs to be a boolean  
  14.             array('rememberMe''boolean'),  
  15.             // password needs to be authenticated  
  16.             array('password''authenticate'),  
  17.             // verifyCode needs to be entered correctly  
  18.             array('verifyCode''captcha''allowEmpty'=>!CCaptcha::checkRequirements()),  
  19.         );  
  20.     }  
  21.   
  22.     /** 
  23.      * Declares attribute labels. 
  24.      */  
  25.     public function attributeLabels()  
  26.     {  
  27.         return array(  
  28.             'rememberMe'=>Yii::t('user',"Remember me next time"),  
  29.             'username'=>Yii::t('user',"username or email"),  
  30.             'password'=>Yii::t('user',"password"),  
  31.             'verifyCode'=>Yii::t('user','Verification Code'),  
  32.         );  
  33.     }  

2,Controller

在LoginController控制器加入映射动作CCaptchaAction

[php] view plaincopy
  1. public function actions()  
  2. {  
  3.     return array(  
  4.         // captcha action renders the CAPTCHA image displayed on the contact page  
  5.         'captcha'=>array(  
  6.             'class'=>'CCaptchaAction',  
  7.             'backColor'=>0xf4f4f4,  
  8.             'padding'=>0,  
  9.             'height'=>30,  
  10.             'maxLength'=>4,  
  11.         ),  
  12.         );  
  13. }  
  14.   
  15. ublic function actionLogin()  
  16. {  
  17.       
  18.     if (Yii::app()->user->isGuest) {  
  19.         $model=new UserLogin;  
  20.         // collect user input data  
  21.         if(isset($_POST['UserLogin']))  
  22.         {  
  23.               
  24.             $model->attributes=$_POST['UserLogin'];  
  25. /在此核对验证码  
  26.             if($this->createAction('captcha')->validate($model->verifyCode, false))  
  27.             {  
  28.                 // validate user input and redirect to previous page if valid  
  29.                 if($model->validate()) {  
  30.                 //admin login only  
  31.                 if( Yii::app()->getModule('user')->isAdmin()==1 )  
  32.                 {  
  33.                 $this->lastViset();  
  34.                 if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)  
  35.                     $this->redirect(Yii::app()->controller->module->returnUrl);  
  36.                 else  
  37.                     $this->redirect(Yii::app()->user->returnUrl);  
  38.                 }else  
  39.                 {//if no admin when login out  
  40.                     $this->redirect(Yii::app()->controller->module->logoutUrl);  
  41.                 }  
  42.             }  
  43.             }else  
  44.             {//提示错误  
  45.                 $model->addError('verifyCode','验证码不对');  
  46.             }  
  47.         }  
  48.         // display the login form  
  49.         $this->render('/user/login',array('model'=>$model));  
  50.     } else  
  51.         $this->redirect(Yii::app()->controller->module->returnUrl);  
  52. }  


在验证用户名密码前,检查验证码:

[php] view plaincopy
  1. if($this->createAction('captcha')->validate($model->verifyCode, false))  
  2.                 {  


3,view

在视图中显示验证码图片,输入框

[php] view plaincopy
  1. <?php $this->widget('CCaptcha'); ?>  
  2.         <?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?>  



[php] view plaincopy
  1. <pre name="code" class="php"><p><img src="http://my.csdn.net/uploads/201205/18/1337330851_3646.jpg" alt="">  
  2. </p><p>---------------------------the end------------------------------------</p></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
0 0