TaoLer/app/middleware/LoginCookie.php
2020-10-16 13:51:56 +08:00

33 lines
685 B
PHP

<?php
namespace app\middleware;
use think\facade\Session;
use think\facade\Cookie;
use think\facade\Db;
class LoginCookie
{
public function handle($request, \Closure $next)
{
//登陆前获取加密的Cookie
$cooAuth = Cookie::get('auth');
if(!empty($cooAuth)){
$resArr = explode(':',$cooAuth);
$userId = end($resArr);
//检验用户
$user = Db::name('user')->where('id',$userId)->find();
if($user){
//验证cookie
$salt = 'taoler';
$auth = md5($user['name'].$salt).":".$userId;
if($auth==$cooAuth){
Session::set('user_name',$user['name']);
Session::set('user_id',$userId);
}
}
}
return $next($request);
}
}