试了很久,都找不出原因,请老师解答
1,开启中间件session
<?php
// 全局中间件定义文件
return [
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class
];
2.在admin文件夹下创建config文件夹,创建admin.php
<?php
return [
'session_admin'=>'adminUsera'
];
3.使用session用config文件夹中admin文件的进行存储
<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\View;
use app\common\model\sql\AdminUser as adminUserOjb;
class Login extends BaseController
{
public function index(){
return View::fetch();
}
public function md5(){
dump(config("admin.session_admin"));
}
public function check(){
if (!$this->request->isPost()){
return show(config("status.error"),"请求方式错误");
}
$username=$this->request->param('username');
$password=$this->request->param('password');
$captcha=$this->request->param('captcha');
if (empty($username) || empty($password) || empty($captcha)){
return show(config("status.error"),"数据不能为空");
}
//判断验证码是否正确
if (!captcha_check($captcha)){
return show(config("status.error"),"验证码不正确");
}
// 判断用户是否存在
$adminUserOjb = new adminUserOjb();
if (empty($adminUser=$adminUserOjb->getUserByName($username))){
return show(config("status.error"),'用户不存在');
}
$adminUser=$adminUser->toArray();
// 判断密码是否正确
if ($adminUser['password']!=md5($password."_singwa_abc")){
return show(config("status.error"),'密码错误');
}
//全部正确则更新登录数据,更新session
$newData=[
"last_login_ip"=>$this->request->ip(),
"update_time"=>time(),
];
if (empty($adminUserOjb->updateAdminData($adminUser['id'],$newData))){
return show(config("status.error"),"登录失败");
};
//全部正确,更新session ----------------------------问题关键-------------------------------
session(config("admin.session_admin"),$adminUser);
return show(config("status.success"),"登录成功");
}
}
43.登录成功后,访问md5方法,返回值为
^ "adminUsera"