打印session,输出null
来源:6-10 后台用户登录-数据更新和session处理
疯狂Rabbit
2020-07-02
admin\config\admin.php
<?php
return [
'session_admin' => 'adminUser',
];
admin\Login.php
<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\BaseController;
use think\facade\View;
use app\common\model\mysql\AdminUser;
class Login extends BaseController{
public function test()
{
halt(session(config("admin.session_admin")));
}
public function index(){
return View::fetch();
}
public function check()
{
if (!$this->request->isPost()){
return rData(config("code.error"),"请求方式错误");
}
$username = $this->request->param('username','','trim');
$password = $this->request->param('password','','trim');
$captcha = $this->request->param('captcha','','trim');
if (empty($username) || empty($password) || empty($captcha)){
return rData(config("code.error"),"参数不能为空");
}
//校验验证码
if (!captcha_check($captcha)){
return rData(config("code.error"),"验证码不正确");
}
try {
$adminModel = new AdminUser;
$adminUser = $adminModel->getAdminUserByUserName($username);
if (empty($adminUser) || $adminUser->status != config("code.mysql.state_normal")) {
return rData(config("code.error"), "管理员不存在");
}
$adminUser = $adminUser->toArray();
if ($adminUser['password'] != password($password, $adminUser['encrypt'])) {
return rData(config("code.error"), "管理员密码错误");
}
//记录登录信息
$updateData = [
"login_time" => time(),
"login_ip" => request()->ip(),
"update_time" => time(),
];
$res = $adminModel->updateById($adminUser['id'], $updateData);
if (empty($res)) {
return rData(config("code.error"), "登录失败");
}
} catch (\Exception $e) {
// todu 记录日志 $e->getMessage();
return rData(config("code.error"), "内部异常,登录失败");
}
//记录session
session(config("admin.session_admin",$adminUser));
return rData(config("code.success"),"登录成功");
}
}
访问 admin/login/test
输出null,请问老师哪里问题
写回答
1回答
-
疯狂Rabbit
提问者
2020-07-02
找到原因了
022022-06-06
相似问题