1、首先在mysql创建一个存用户的表格

create table test_user
(
user_id bigint(20) unsigned not null auto_increment comment 'ID',
user_email varchar(100) not null comment '电子邮件',
user_password varchar(100) not null comment '密码',
user_access_token varchar(200) comment 'access_token',
user_auth_key varchar(200) comment 'auth_key',
user_create_time datetime comment '创建时间',
primary key(user_id)
);

2、在表中插入一条登陆的账号

3、新建模型models/MysqlUser.php

<?php

namespace app\models;

use yii\db\ActiveRecord;
use yii\web\IdentityInterface; class MysqlUser extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
//相应的表名
return 'test_user';
} public static function findIdentity($id)
{
//自己主动登陆时会调用
$temp = parent::find()->where(['user_id'=>$id])->one();
return isset($temp)?new static($temp):null;
} public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['user_access_token' => $token]);
} public function getId()
{
return $this->user_id;
} public function getAuthKey()
{
return $this->user_auth_key;
} public function validateAuthKey($authKey)
{
return $this->user_auth_key === $authKey;
} public function validatePassword($password)
{
return $this->user_password === $password;
}
}

4、新建模型models/MloginForm.php

<?php

namespace app\models;

use Yii;
use yii\base\Model; //加上这一句,引用
use app\models\MysqlUser; class MloginForm extends Model
{
public $email;
public $password; private $_user = false; public function rules()
{
return [
['email','email','message'=>'必须是邮件格式'],
[['email','password'],'required','message'=>'必填'],
['password','validatePassword','message'=>'账号或密码不对'],
];
} //登陆
public function login()
{
if ($this->validate())
return Yii::$app->user->login($this->getUser(), 3600*24*30);
else
return false;
} //推断账号密码是否正确
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors())
{
$user = $this->getUser(); if (!$user)
{
$this->addError($attribute, '账号或密码不对');
} }
} //依据邮箱和密码查询数据库
public function getUser()
{
if ($this->_user === false)
{
$this->_user = MysqlUser::find()->where(['user_email'=>$this->email,'user_password'=>$this->password])->one();
}
return $this->_user;
} } ?>

5、新建视图views/account/login.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php
echo '<h1>'.$status.'</h1>';
?>
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'email'); ?>
<?php echo $form->field($model, 'password'); ?>
<?php echo Html::submitButton('登陆'); ?>
<?php ActiveForm::end(); ?>

6、新建控制器controllers/AccountController.php

<?php
namespace app\controllers; use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter; //引用
use app\models\MloginForm; class AccountController extends Controller
{
function actionLogin()
{
$model = new MloginForm();
if($model->load(Yii::$app->request->post()))
{
if($model->login())
//登陆成功
return $this->renderPartial('login',['model'=>$model,'status'=>'成功']);
else
//登陆失败
return $this->renderPartial('login',['model'=>$model,'status'=>'失败']);
}
else
{
return $this->renderPartial('login',['model'=>$model,'status'=>'']);
}
}
}

另外,自己主动登陆配置的地方是config/web.php

效果例如以下所看到的

点击登陆button后

若账号password不对

自己主动登陆还有点问题,等之后解决。

转载请注明出处:http://blog.csdn.net/zhyoulun/article/details/40687545

Yii Framework2.0开发教程(10)配合mysql数据库实现用户登录的更多相关文章

  1. Yii Framework2.0开发教程(5)数据库mysql性能

    继续<Yii Framework2.0开发教程(3)数据库mysql入门> 首先给予一定的尊重yii2数据库支持引进 Yii 基于 PHP's PDO一个成熟的数据库访问层的建立.它提供了 ...

  2. Yii Framework2.0开发教程(3)数据库mysql入门

    沿用教程(2)的代码 第一步.在本地mysql数据库中新建数据库zhyoulun 第二步.在数据库中新建表并插入若干条数据 CREATE TABLE `country` ( `code` CHAR(2 ...

  3. Yii Framework2.0开发教程(1)配置环境及第一个应用HelloWorld

    准备工作: 我用的开发环境是windows下的apache+mysql+php 编辑器不知道该用哪个好.临时用dreamweaver吧 我自己的http://localhost/相应的根文件夹是E:/ ...

  4. Yii Framework2.0开发教程(2)使用表单Form

    第一步.接着教程(1).我们在controllers/ZhyoulunController.php中加入两处, 1) use app\models\EntryForm; 和 2) public fun ...

  5. Yii Framework2.0开发教程(4)在yii中定义全局变量

    在yii中定义全局变量最好的地方是入口脚本处.也就是web目录中的index.php文件 比如我们在defined('YII_ENV') or define('YII_ENV', 'dev');后写上 ...

  6. 【转】10 个MySQL数据库备份教程推荐

    10 个MySQL数据库备份教程推荐 MySQL是动态网站开发中最著名的开源数据库系统.如果你在网站中使用了MySQL,那么你应该定期备份你的数据以防止它丢失. 本文将介绍自动或手动备份MySQL数据 ...

  7. Senparc.Weixin.MP SDK 微信公众平台开发教程(七):解决用户上下文(Session)问题

    从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同一个微信服务器,这使得常规的Session无法使用(始终面对同一个请求对象,况且还有对方服务器Co ...

  8. mysql数据库优化课程---10、mysql数据库分组聚合

    mysql数据库优化课程---10.mysql数据库分组聚合 一.总结 一句话总结:select concat(class,' 班') 班级,concat(count(*),' 人') 人数 from ...

  9. 高并发大流量专题---10、MySQL数据库层的优化

    高并发大流量专题---10.MySQL数据库层的优化 一.总结 一句话总结: mysql先考虑做分布式缓存,过了缓存后就做mysql数据库层面的优化 1.mysql数据库层的优化的前面一层是什么? 数 ...

随机推荐

  1. Java设计模式菜鸟系列(两)建模与观察者模式的实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39755577 观察者(Observer)模式定义:在对象之间定义了一对多的依赖关系,这样一 ...

  2. 【Java基础】对象的具体创建过程

    所有的类(以Dog类为例)在第一次使用时,动态的加载到JVM中,当首次创建Dog对象时,或者是Dog类的静态方法.静态属性域在第一次被访问时,JVM解释器查找到classpath,定位到Dog.cla ...

  3. A == B ?(杭州电2054)

    A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. POJ3467(预处理)

    Cross Counting Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1331   Accepted: 375 De ...

  5. 解决因特网和xshell考虑到问题

    首先需要解释.我们学校的网络是免费的.无论是实验室或宿舍.因此,互联网是基于Mac地址分配IP的,所以我VirtualBox安装了centos之后,话.就须要将VirtualBox的mac地址改成和我 ...

  6. Task的异步模式

    Task的异步模式 返回该系列目录<基于Task的异步模式--全面介绍> 生成方法 编译器生成 在.NET Framework 4.5中,C#编译器实现了TAP.任何标有async关键字的 ...

  7. Android 设计模式Template Method模式

    自定义模板方法模式:定义的算法的骨架中的方法,虽然某些步骤推迟到子类中,下模板方法允许子类不能改变在的情况下,该算法的结构.算法重新定义某些步骤. 设计原则:不要给我们打电话.我会打电话给你.(像猎头 ...

  8. Oracle压缩总结2— 估计表压缩效应

    使用压缩前,我们可以估算压缩能有多大效果. 11gr2我已经能够使用dbms_comp_advisor,具体代码见附件.只需要运行两个文件dbmscomp.sql和prvtcomp.plb.然后使用D ...

  9. SharePoint 2013 配置开发环境,需安装VS2012插件

    原文:SharePoint 2013 配置开发环境,需安装VS2012插件 SharePoint 2013已经安装好了,接下来就是配置开发环境,安装VS2012,但是,装好了以后,发现没有ShareP ...

  10. Explicit keyword

    说实话,从来没有感觉到这个keyword实用,直到今天. explicit的意思是明显的,和它相相应的一个词是implicit意思是隐藏的. 我參考了MSDN和<c++标准程序库>对这个k ...