本文实例讲述了Yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下:
捣鼓了一下午,总算走通了,下面贴出代码。
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'=>'请输入正确的验证码'),
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' => '记住登录状态',
'verifyCode' => '验证码'
);
}
}
注:IndexForm也可以写成LoginForm,只是系统内已经有了,我就没有替换它,同时注意看自己用户表的字段,一般是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');
}
}
注:第一个方法是添加验证码的
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(); ?>
Laravel下生成验证码的类这篇文章主要为大家详细介绍了Laravel下生成验证码的类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
PHP实现验证码校验功能这篇文章主要为大家详细介绍了PHP实现验证码校验功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
php利用云片网实现短信验证码功能的示例代码这篇文章主要介绍了php利用云片网实现短信验证码功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家
PHP验证码类文件及调用方式代码详解这篇文章主要介绍了PHP验证码类文件及调用方式代码详解,需要的朋友可以参考下
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)下面小编就为大家带来一篇修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)。小编觉得挺不错的,现在就分
一个实用的php验证码类这篇文章主要为大家详细介绍了一个实用的php验证码类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下