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

來源:互聯網
上載者:User

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)); ?>

希望本文所述對大家基於Yii架構的PHP程式設計有所協助。

您可能感興趣的文章:

  • yii使用者註冊表單驗證執行個體
  • PHP Yii架構之表單驗證規則大全
  • Yii架構form表單用法執行個體
  • Yii不依賴Model的表單產生器用法執行個體
  • yii架構表單模型使用及以數組形式提交表單資料樣本
  • Yii中實現處理前後台登入的新方法
  • yii去掉必填項中星號的方法

http://www.bkjia.com/PHPjc/1085886.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1085886.htmlTechArticleYii使用Captcha驗證碼的方法,yiicaptcha驗證碼 本文執行個體講述了Yii使用Captcha驗證碼的方法。分享給大家供大家參考,具體如下: 詳細代碼可參...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.