当前位置:首页 > CMS教程 > 其它CMS > 列表

Yii使用Captcha验证码的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-02 11:23:23 浏览: 评论:0 

这篇文章主要介绍了Yii使用Captcha验证码的方法,结合实例形式分析了Yii使用Captcha验证码的MVC三层具体实现技巧,需要的朋友可以参考下。

本文实例讲述了Yii使用Captcha验证码的方法,分享给大家供大家参考,具体如下:

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

1. Model:

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

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

2. Controller

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

  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. ublic function actionLogin() 
  15. { 
  16.  if (Yii::app()->user->isGuest) { 
  17.   $model=new UserLogin; 
  18.   // collect user input data 
  19.   if(isset($_POST['UserLogin'])) 
  20.   { 
  21.    $model->attributes=$_POST['UserLogin']; 
  22. //在此核对验证码 
  23.    if($this->createAction('captcha')->validate($model->verifyCode, false)) 
  24.    { 
  25.     // validate user input and redirect to previous page if valid 
  26.     if($model->validate()) { 
  27.     //admin login only 
  28.     if( Yii::app()->getModule('user')->isAdmin()==1 ) 
  29.     { 
  30.     $this->lastViset(); 
  31.     if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false) 
  32.      $this->redirect(Yii::app()->controller->module->returnUrl); 
  33.     else 
  34.      $this->redirect(Yii::app()->user->returnUrl); 
  35.     }else 
  36.     {//if no admin when login out 
  37.      $this->redirect(Yii::app()->controller->module->logoutUrl); 
  38.     } 
  39.    } 
  40.    }else 
  41.    {//提示错误 
  42.     $model->addError('verifyCode','验证码不对'); 
  43.    } 
  44.   } 
  45.   // display the login form 
  46.   $this->render('/user/login',array('model'=>$model)); 
  47.  } else 
  48.   $this->redirect(Yii::app()->controller->module->returnUrl); 
  49. } 

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

if($this->createAction('captcha')->validate($model->verifyCode, false))

{

3. view

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

  1. <?php $this->widget('CCaptcha'); ?> 
  2.   <?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?> 
  3. <img src="http://www.XXXX.net/uploads/123456.jpg" alt=""> 

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

Tags: Captcha Yii验证码

分享到: