Yii使用Captcha驗(yàn)證碼的方法
本文實(shí)例講述了Yii使用Captcha驗(yàn)證碼的方法。分享給大家供大家參考,具體如下:
詳細(xì)代碼可參考:yii自帶的示例代碼post項(xiàng)目,里面有一個(gè)contact表單用到了驗(yàn)證碼.
1. Model:
將驗(yàn)證碼加入U(xiǎn)serLogin的一個(gè)屬性:
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控制器加入映射動(dòng)作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'];
//在此核對(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)證用戶(hù)名密碼前,檢查驗(yàn)證碼:
if($this->createAction('captcha')->validate($model->verifyCode, false))
{
3. view
在視圖中顯示驗(yàn)證碼圖片,輸入框
<?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ì)有所幫助。
- Yii2框架實(shí)現(xiàn)登陸添加驗(yàn)證碼功能示例
- Yii2 如何在modules中添加驗(yàn)證碼的方法
- Yii2下點(diǎn)擊驗(yàn)證碼的切換實(shí)例代碼
- Yii2簡(jiǎn)單實(shí)現(xiàn)給表單添加驗(yàn)證碼的方法
- Yii2增加驗(yàn)證碼步驟詳解
- yii2中添加驗(yàn)證碼的實(shí)現(xiàn)方法
- Yii1.0 不同頁(yè)面多個(gè)驗(yàn)證碼的使用實(shí)現(xiàn)
- Yii 2.0自帶的驗(yàn)證碼使用經(jīng)驗(yàn)分享
- Yii輸入正確驗(yàn)證碼卻驗(yàn)證失敗的解決方法
- Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能示例
- yii實(shí)現(xiàn)創(chuàng)建驗(yàn)證碼實(shí)例解析
- YII2框架中驗(yàn)證碼的簡(jiǎn)單使用方法示例
相關(guān)文章
使用PHP實(shí)現(xiàn)阻止用戶(hù)上傳成人照片或者裸照
這篇文章主要介紹了使用PHP實(shí)現(xiàn)阻止用戶(hù)上傳成人照片或者裸照,方法和思路都非常的不錯(cuò),推薦給大家,需要的朋友可以參考下2014-12-12
php array_udiff_assoc 計(jì)算兩個(gè)數(shù)組的差集實(shí)例
這篇文章主要介紹了php array_udiff_assoc 計(jì)算兩個(gè)數(shù)組的差集實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11
laravel orm 關(guān)聯(lián)條件查詢(xún)代碼
今天小編就為大家分享一篇laravel orm 關(guān)聯(lián)條件查詢(xún)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHPStorm2020.1永久激活及下載更新至2020(推薦)
這篇文章主要介紹了PHPStorm2020.1永久激活及下載更新至2020,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
PHP和javascript常用正則表達(dá)式及用法實(shí)例
這篇文章主要介紹了常用的PHP和javascript正則表達(dá)式及用法實(shí)例,精心收集的PHP和javascript正則表達(dá)式各10個(gè),需要的朋友可以參考下2014-07-07

