老师,我写入空方法public function _initialize() {}后,还是无法解决死循环的问题
来源:3-7 后台权限控制功能的实现

FasMsa
2020-03-17
在登录页面上成功验证用户名、密码已经验证码后,跳转时发生错误
Login.php
<?php
namespace app\admin\controller;
use app\common\validate\AdminUser;
use think\Controller;
use app\common\lib\IAuth;
class Login extends Base
{
public function _initialize() {
}
public function index()
{
$isLogin = $this->isLogin();
if($isLogin) {
return $this->redirect('index/index');
}else {
//若后台用户已登录,则需跳到后台页面
return $this->fetch();
}
}
// 登录相关业务
public function check() {
if(request()->isPost()) {
$data = input('post.');
if (!captcha_check($data['code'])) {
$this->error('验证码不正确');
}
//判定 username password
//validate机制
try {
// username username+password
$user = model('AdminUser')->get(['username' => $data['username']]);
}catch (\Exception $e) {
$this->error($e->getMessage());
}
if (!$user || $user->status != config('code.status_normal')) {
$this->error('该用户不存在');
}
//再对密码进行校验
if (IAuth::setPassword($data['password']) != $user['password']) {
$this->error('密码不正确');
}
// 1 更新数据库 登录时间 登录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');
//halt($user);
}else {
$this->error('请求不合法');
}
}
public function welcome() {
return "hello api-admin";
}
//退出登录的逻辑
// 1 清空session
// 2 跳转到登录页面
public function logout() {
session(null, config('admin.session_user_scope'));
//跳转
$this->redirect('login/index');
}
}
Index.php
<?php
namespace app\admin\controller;
use think\Controller;
class Index extends Base
{
public function index()
{
//halt(session(config('admin.session_user'), '', config('admin.session_user_scope')));
return $this->fetch();
}
public function welcome() {
return "hello api-admin";
}
}
Base.php
<?php
namespace app\admin\controller;
use think\Controller;
//后台基础类库
class Base extends Controller
{
//初始化的方法
public function _initialize()
{
//判定用户是否登录
$isLogin = $this->isLogin();
if($isLogin) {
return $this->redirect('login/index');
}
}
//判定是否登录
public function isLogin() {
//获取session
$user = session(config('admin.session_user'), '', config('admin.session_user_scope'));
if($user && $user->id) {
return true;
}
return false;
}
}
我的php版本是7.3.4
写回答
1回答
-
singwa
2020-03-19
您好。您有云服务器吗,我上您云服务器看看。这样排查问题效率高点。
012020-03-19
相似问题