Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能示例
本文實(shí)例講述了Yii框架實(shí)現(xiàn)的驗(yàn)證碼、登錄及退出功能。分享給大家供大家參考,具體如下:
搗鼓了一下午,總算走通了,下面貼出代碼。
Model
<?php
class Auth extends CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{auth}}';
}
}
注:我的用戶表是auth,所以模型是Auth.php
<?php
class IndexForm extends CFormModel {
public $a_account;
public $a_password;
public $rememberMe;
public $verifyCode;
public $_identity;
public function rules() {
return array(
array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message'=>'請輸入正確的驗(yàn)證碼'),
array('a_account', 'required', 'message' => '用戶名必填'),
array('a_password', 'required', 'message' => '密碼必填'),
array('a_password', 'authenticate'),
array('rememberMe', 'boolean'),
);
}
public function authenticate($attribute, $params) {
if (!$this->hasErrors()) {
$this->_identity = new UserIdentity($this->a_account, $this->a_password);
if (!$this->_identity->authenticate()) {
$this->addError('a_password', '用戶名或密碼不存在');
}
}
}
public function login() {
if ($this->_identity === null) {
$this->_identity = new UserIdentity($this->a_account, $this->a_password);
$this->_identity->authenticate();
}
if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
$duration = $this->rememberMe ? 60*60*24*7 : 0;
Yii::app()->user->login($this->_identity, $duration);
return true;
} else {
return false;
}
}
public function attributeLabels() {
return array(
'a_account' => '用戶名',
'a_password' => '密碼',
'rememberMe' => '記住登錄狀態(tài)',
'verifyCode' => '驗(yàn)證碼'
);
}
}
注:IndexForm也可以寫成LoginForm,只是系統(tǒng)內(nèi)已經(jīng)有了,我就沒有替換它,同時(shí)注意看自己用戶表的字段,一般是password和username,而我的是a_account和a_password
Controller
<?php
class IndexController extends Controller {
public function actions() {
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'width'=>100,
'height'=>50
)
);
}
public function actionLogin() {
if (Yii::app()->user->id) {
echo "<div>歡迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";
} else {
$model = new IndexForm();
if (isset($_POST['IndexForm'])) {
$model->attributes = $_POST['IndexForm'];
if ($model->validate() && $model->login()) {
echo "<div>歡迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";exit;
}
}
$this->render('login', array('model' => $model));
}
}
public function actionLogout() {
Yii::app()->user->logout();
$this->redirect(SITE_URL . 'admin/index/login');
}
}
注:第一個(gè)方法是添加驗(yàn)證碼的
view
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'login-form',
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true
)
));
?>
<div class="row">
<?php echo $form->labelEx($model,'a_account'); ?>
<?php echo $form->textField($model,'a_account'); ?>
<?php echo $form->error($model,'a_account'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'a_password'); ?>
<?php echo $form->passwordField($model,'a_password'); ?>
<?php echo $form->error($model,'a_password'); ?>
</div>
<?php if(CCaptcha::checkRequirements()) { ?>
<div class="row">
<?php echo $form->labelEx($model, 'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model, 'verifyCode'); ?>
<?php echo $form->error($model, 'verifyCode'); ?>
</div>
<?php } ?>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
同時(shí)修改項(xiàng)目下protected/components下的UserIdentity.php
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
/*
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
*/
$user_model = Auth::model()->find('a_account=:name',array(':name'=>$this->username));
if($user_model === null){
$this -> errorCode = self::ERROR_USERNAME_INVALID;
return false;
} else if ($user_model->a_password !== md5($this -> password)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
return false;
} else {
$this->errorCode=self::ERROR_NONE;
return true;
}
}
}
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- Yii2框架實(shí)現(xiàn)登陸添加驗(yàn)證碼功能示例
- Yii2 如何在modules中添加驗(yàn)證碼的方法
- Yii2下點(diǎn)擊驗(yàn)證碼的切換實(shí)例代碼
- Yii2簡單實(shí)現(xiàn)給表單添加驗(yàn)證碼的方法
- Yii2增加驗(yàn)證碼步驟詳解
- yii2中添加驗(yàn)證碼的實(shí)現(xiàn)方法
- Yii1.0 不同頁面多個(gè)驗(yàn)證碼的使用實(shí)現(xiàn)
- Yii 2.0自帶的驗(yàn)證碼使用經(jīng)驗(yàn)分享
- Yii輸入正確驗(yàn)證碼卻驗(yàn)證失敗的解決方法
- Yii使用Captcha驗(yàn)證碼的方法
- yii實(shí)現(xiàn)創(chuàng)建驗(yàn)證碼實(shí)例解析
- YII2框架中驗(yàn)證碼的簡單使用方法示例
相關(guān)文章
php使用phpoffice/phpspreadsheet拓展操作excel實(shí)例
這篇文章主要為大家介紹了php使用phpoffice/phpspreadsheet拓展操作excel實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
PHP中的常見魔術(shù)方法功能作用及用法實(shí)例
這篇文章主要介紹了PHP中的常見魔術(shù)方法功能作用及用法實(shí)例,本文講解了構(gòu)造函數(shù)和析構(gòu)函數(shù)__construct()和__desctruct()以及屬性重載(Property Overloading)__get()和、__set()、__isset()等等魔術(shù)方法,需要的朋友可以參考下2015-07-07
thinkPHP多表查詢及分頁功能實(shí)現(xiàn)方法示例
這篇文章主要介紹了thinkPHP多表查詢及分頁功能實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了thinkPHP多表查詢以及查詢結(jié)果的分頁顯示相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
使用openssl實(shí)現(xiàn)rsa非對稱加密算法示例
這篇文章主要介紹了使用openssl實(shí)現(xiàn)rsa非對稱加密算法的示例,大家參考使用吧2014-01-01
laravel中Join語法以及使用Join多個(gè)條件
這篇文章主要介紹了laravel中Join語法以及使用Join多個(gè)條件,文中不僅介紹了join用法而且講述了多種方法很詳細(xì),有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03
詳解如何實(shí)現(xiàn)phpoffice的excel導(dǎo)入功能解耦
這篇文章主要為大家介紹了詳解如何實(shí)現(xiàn)phpoffice的excel導(dǎo)入功能解耦,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
laravel 錯(cuò)誤處理,接口錯(cuò)誤返回json代碼
今天小編就為大家分享一篇laravel 錯(cuò)誤處理,接口錯(cuò)誤返回json代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP動(dòng)態(tài)生成javascript文件的2個(gè)例子
這篇文章主要介紹了PHP動(dòng)態(tài)生成javascript文件的2個(gè)例子,需要的朋友可以參考下2014-04-04
thinkphp5+layui實(shí)現(xiàn)的分頁樣式示例
這篇文章主要介紹了thinkphp5+layui實(shí)現(xiàn)的分頁樣式,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP5框架結(jié)合layui實(shí)現(xiàn)的分頁功能相關(guān)的配置、查詢等操作技巧,需要的朋友可以參考下2019-10-10
php循環(huán)創(chuàng)建目錄示例分享(php創(chuàng)建多級(jí)目錄)
這篇文章主要介紹了php循環(huán)創(chuàng)建目錄示例,原理就是不斷的嘗試創(chuàng)建上層目錄,依此類推,需要的朋友可以參考下2014-03-03

