php不同用户登陆权限,[PHP高可用后端]⑩--登陆权限控制

2bebfb5573fc

Paste_Image.png

2bebfb5573fc

Paste_Image.png

Base.php

pp\admin\controller;

use think\Controller;

/**

* 后台登陆基类

* Class Base

* @package app\admin\controller

*/

class Base extends Controller

{

/**

* 初始化方法

*/

protected function _initialize()

{

//判断用户是否登陆

$isLogin = $this->isLogin();

if (!$isLogin) {

return $this->redirect('login/index');

}

}

/**

* 判断是否登陆

* @return bool

*/

public function isLogin()

{

//获取session

$user = session(config('admin.session_user'), '', config('admin.session_user_scope'));

if ($user && $user->id) {

return true;

}

return false;

}

}

Login.php

/**

* Created by PhpStorm.

* User: tong

* Date: 2017/11/1

* Time: 14:25

*/

namespace app\admin\controller;

use app\common\lib\IAuth;

class Login extends Base

{

protected function _initialize()

{

//避免死循环

}

public function index()

{

//如果后台已经登陆,直接跳道首页

$isLogin = $this->isLogin();

if ($isLogin) {

return $this->redirect('index/index');

}

return $this->fetch();

}

public function check()

{

if (request()->isPost()) {

$data = input('post.');

if (!captcha_check($data['code'])) {

$this->error('验证码不正确');

}

$validate = validate('Login');

if (!$validate->check($data)) {

$this->error($validate->getError());

}

try {

$user = model('AdminUser')->get(

['username' => $data['username']]

);

} catch (\Exception $e) {

$this->error($e->getMessage());

}

if (!$user || $user->status != config('code.status_normal')) {

// $this->error 内部会throw一个Exception 所以不需要放在try catch中

$this->error('该用户不存在');

}

if (IAuth::setPassword($data['password']) != $user['password']) {

$this->error("密码不正确");

}

//更新数据库 登陆时间 登陆ip

$udata = [

'last_login_time' => time(),

'last_login_ip' => request()->ip(),

];

try {

model('AdminUser')->save($udata, ['id' => $user->id]);

} catch (\Exception $e) {

$this->error($e->getMessage());

}

//2.session

session(config('admin.session_user'), $user, config('admin.session_user_scope'));

$this->success('登陆成功', 'index/index');

} else {

$this->error("请求不合法");

}

}

/**

* 清空登陆的逻辑

* 1.清空session

* 2.跳转到登陆页面

*/

public function logout()

{

//清空作用域

session(null, config('admin.session_user_scope'));

//跳转

$this->redirect('login/index');

}

}

protected function _initialize()

{

//避免死循环

}

public function index()

{

//如果后台已经登陆,直接跳道首页

$isLogin = $this->isLogin();

if ($isLogin) {

return $this->redirect('index/index');

}

return $this->fetch();

}

Index.php

class Index extends Base{

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值