首页 > PHP教程 > php开发知识文章

php -- Yii框架用户的登录、退出功能以及验证码实现

这篇文章主要介绍了php -- Yii框架用户的登录、退出功能以及验证码实现方法,

结合具体实例形式分析了基于Yii框架实现的用户验证登录及退出操作相关步骤与操作技巧

模型Auth.php

class User extends CActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}

public function tableName()
{
return '{{user}}';
}
}

用户表是user,所以模型是user.php

class IndexForm extends CFormModel
{
public $account;
public $password;
public $rememberMe;
public $verifyCode;
public $_identity;

public function rules()
{
return array(
array(
'verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(),
'message' => '请输入正确的验证码'),
array('account', 'required', 'message' => '用户名必填'),
array('password', 'required', 'message' => '密码必填'),
array('password', 'authenticate'),
array('rememberMe', 'boolean'),);
}

public function authenticate($attribute, $params)
{
if (!$this->hasErrors()) {
$this->_identity = new UserIdentity($this->account, $this->password);
if (!$this->_identity->authenticate()) {
$this->addError('password', '用户名或密码不存在');
}
}
}

public function login()
{
if ($this->_identity === null) {
$this->_identity = new UserIdentity($this->account, $this->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(
'account' => '用户名',
'password' => '密码',
'rememberMe' => '记住登录状态',
'verifyCode' => '验证码'
);
}
}

控制器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 . "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 . "index/logout'>退出</a></div>";
exit;
}
}
$this->render('login', array('model' => $model));
}
}

public function actionLogout()
{
Yii::app()->user->logout();
$this->redirect(SITE_URL . 'index/login');
}
}

第一个方法是添加验证码的

视图php文件

<html>
<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, 'account'); ?>
<?php echo $form->textField($model, 'account'); ?>
<?php echo $form->error($model, 'account'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'password'); ?>
<?php echo $form->passwordField($model, 'password'); ?>
<?php echo $form->error($model, '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(); ?>
</html>

修改项目下protected/components下的UserIdentity.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()
{
$user_model = User::model()->find('account=:name', array(':name' => $this->username));
if ($user_model === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
return false;
} else if ($user_model->password !== md5($this->password)) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
return false;
} else {
$this->errorCode = self::ERROR_NONE;
return true;
}
}
}

以上就是本文php -- Yii框架用户的登录、退出功能以及验证码实现的全部内容,希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

PHP Yii框架介绍

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。