程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> Yii使用Captcha驗證碼的方法,yiicaptcha驗證碼

Yii使用Captcha驗證碼的方法,yiicaptcha驗證碼

編輯:關於PHP編程

Yii使用Captcha驗證碼的方法,yiicaptcha驗證碼


本文實例講述了Yii使用Captcha驗證碼的方法。分享給大家供大家參考,具體如下:

詳細代碼可參考:yii自帶的示例代碼post項目,裡面有一個contact表單用到了驗證碼.

1. Model:

將驗證碼加入UserLogin的一個屬性:

class UserLogin extends CFormModel
{
 public $username;
 public $password;
 public $rememberMe;
 public $verifyCode;
 public function rules()
 {
  return array(
   // username and password are required
   array('username, password,verifyCode', 'required'),
   // rememberMe needs to be a boolean
   array('rememberMe', 'boolean'),
   // password needs to be authenticated
   array('password', 'authenticate'),
   // verifyCode needs to be entered correctly
   array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
  );
 }
 /**
  * Declares attribute labels.
  */
 public function attributeLabels()
 {
  return array(
   'rememberMe'=>Yii::t('user',"Remember me next time"),
   'username'=>Yii::t('user',"username or email"),
   'password'=>Yii::t('user',"password"),
   'verifyCode'=>Yii::t('user','Verification Code'),
  );
 }
}

2. Controller

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

public function actions()
{
 return array(
  // captcha action renders the CAPTCHA image displayed on the contact page
  'captcha'=>array(
   'class'=>'CCaptchaAction',
   'backColor'=>0xf4f4f4,
   'padding'=>0,
   'height'=>30,
   'maxLength'=>4,
  ),
  );
}
ublic function actionLogin()
{
 if (Yii::app()->user->isGuest) {
  $model=new UserLogin;
  // collect user input data
  if(isset($_POST['UserLogin']))
  {
   $model->attributes=$_POST['UserLogin'];
//在此核對驗證碼
   if($this->createAction('captcha')->validate($model->verifyCode, false))
   {
    // validate user input and redirect to previous page if valid
    if($model->validate()) {
    //admin login only
    if( Yii::app()->getModule('user')->isAdmin()==1 )
    {
    $this->lastViset();
    if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
     $this->redirect(Yii::app()->controller->module->returnUrl);
    else
     $this->redirect(Yii::app()->user->returnUrl);
    }else
    {//if no admin when login out
     $this->redirect(Yii::app()->controller->module->logoutUrl);
    }
   }
   }else
   {//提示錯誤
    $model->addError('verifyCode','驗證碼不對');
   }
  }
  // display the login form
  $this->render('/user/login',array('model'=>$model));
 } else
  $this->redirect(Yii::app()->controller->module->returnUrl);
}

在驗證用戶名密碼前,檢查驗證碼:

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

3. view

在視圖中顯示驗證碼圖片,輸入框

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

希望本文所述對大家基於Yii框架的PHP程序設計有所幫助。

您可能感興趣的文章:

  • yii用戶注冊表單驗證實例
  • PHP Yii框架之表單驗證規則大全
  • Yii框架form表單用法實例
  • Yii不依賴Model的表單生成器用法實例
  • yii框架表單模型使用及以數組形式提交表單數據示例
  • Yii中實現處理前後台登錄的新方法
  • yii去掉必填項中星號的方法

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved