《PHP應(yīng)用:Yii使用Captcha驗(yàn)證碼的方法》要點(diǎn):
本文介紹了PHP應(yīng)用:Yii使用Captcha驗(yàn)證碼的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
本文實(shí)例講述了Yii使用Captcha驗(yàn)證碼的辦法.分享給大家供大家參考,具體如下:PHP學(xué)習(xí)
詳細(xì)代碼可參考:yii自帶的示例代碼post項(xiàng)目,里面有一個(gè)contact表單用到了驗(yàn)證碼.PHP學(xué)習(xí)
1. Model:PHP學(xué)習(xí)
將驗(yàn)證碼加入U(xiǎn)serLogin的一個(gè)屬性:PHP學(xué)習(xí)
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. ControllerPHP學(xué)習(xí)
在LoginController控制器加入映射動(dòng)作CCaptchaActionPHP學(xué)習(xí)
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']; //在此核對(duì)驗(yàn)證碼 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 {//提示錯(cuò)誤 $model->addError('verifyCode','驗(yàn)證碼不對(duì)'); } } // display the login form $this->render('/user/login',array('model'=>$model)); } else $this->redirect(Yii::app()->controller->module->returnUrl); }
在驗(yàn)證用戶名暗碼前,檢查驗(yàn)證碼:PHP學(xué)習(xí)
if($this->createAction('captcha')->validate($model->verifyCode, false)) {
3. viewPHP學(xué)習(xí)
在視圖中顯示驗(yàn)證碼圖片,輸入框PHP學(xué)習(xí)
<?php $this->widget('CCaptcha'); ?> <?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?> <img src="http://www.XXXX.net/uploads/123456.jpg" alt="">
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所贊助.PHP學(xué)習(xí)
《PHP應(yīng)用:Yii使用Captcha驗(yàn)證碼的方法》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP應(yīng)用:Yii使用Captcha驗(yàn)證碼的方法》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7980.html