# TP5实战源码 --- 全局用户信息验证类Auth [TOC] >[info] 在实战开发中,把登录信息写入session是一个非常方便而又安全的解决方案 > 但开发大型项目前端需要多台应用机时候开启第三方CDN加速 或者 启用反向代理 ,session就不能解> > 决这个问题了.而完全使用cookie则是非常不安全的. > 这个时候 ,我们就需要一台共享缓存服务器来解决这个问题,memcache服务器就是一个很好的共享缓存服务器.当然,你也可以选择redis服务器. ## 登录信息常用的解决方案 * 用户登录时: 创建session_sid ,用cookie在顾客端存储session_sid ,服务器端 存储session_sid对应的用户信息 * 检查登录时: 首先获取cookie顾客端存储session_sid,再通过session_sid去查询缓存中的用户信息 * 用户退出时: 清除用户cookie和服务器端对应的用户缓存信息 这样,只要服务器集群中的缓存是共享的,就能解决该问题 ## 登录信息常用的源码 * 新建Auth 验证类 ~~~ <?php /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/2/20 * Time: 8:44 */ namespace app\api\controller; use think\Cache; use think\Config; use think\Cookie; use think\Session; class Auth extends Base { protected $loginType; //存储登陆信息类型 session cache redis protected $uuid; protected $member_info; /** * 构造函数 开启checkLoginGlobal * Power by Mikkle * QQ:776329498 */ public function _initialize() { parent::_initialize(); // TODO: Change the autogenerated stub //获取config中定义的登录类型 "login_type" $this->loginType = Config::get("login_type") ? Config::get("login_type") : "session"; $this->checkLoginGlobal(); } } ~~~ > 我在config文件中设置了登录类型 "login_type",默认值session > 获取方式 Config::get("login_type") * 检查全局登录 ~~~ /** * 检测是否登录 * Power by Mikkle * QQ:776329498 * @return bool */ public function checkLoginGlobal() { $check_success = false; switch ($this->loginType) { case 1; case "session"; $this->uuid = Session::get('uuid', 'Global'); $this->member_info = Session::get('member_info', 'Global'); if ($this->uuid && $this->member_info) { $check_success = true; } break; case 2; case "cache"; $session_id_check = Cookie::get("session_id"); $this->uuid = Cache::get("uuid_{$session_id_check}"); $this->member_info = Cache::get("member_info_{$session_id_check}"); if ($this->uuid && $this->member_info) { $check_success = true; } //刷新 缓存有效期 Cache::set("uuid_{$session_id_check}", $this->uuid); Cache::set("member_info_{$session_id_check}", $this->member_info); break; case 3: case "redis": break; } return $check_success; } ~~~ * 设置全局登录 ~~~ /** * 设置全局登录 * #User: Mikkle * #Email:776329498@qq.com * #Date: */ public function setLoginGlobal($member_info = [], $login_code = 0) { $set_success = false ; if ($member_info) { switch ($this->loginType) { case 1: case "session": Session::set('member_info', $member_info, 'Global'); Session::set('uuid', $member_info['uuid'], 'Global'); if ((Session::has("uuid", "Global"))) { $set_success = true; } break; case 2: case "cache": $session_id = $this->create_uuid("SN"); Cookie::set("session_id", $session_id); Cache::set("member_info_$session_id", $member_info); Cache::set("uuid_$session_id", $member_info['uuid']); $session_id_check = Cookie::get("session_id"); if ((Cache::get("uuid_{$session_id_check}"))) { $set_success = true; } break; case 3:case "redis": break; } } if (!$set_success) return false; //保存登录记录 $this->saveLoginInfo($member_info['uuid'],$login_code); return true; } ~~~ * 设置全局退出 ~~~ /** * 全局退出 * Power by Mikkle * QQ:776329498 * @return bool */ protected function logoutGlobal(){ switch ($this->loginType) { case 1: case "session": Session::delete('uuid', 'Global'); Session::delete('member_info', 'Global'); break; case 2: case "cache": $session_id_check = Cookie::get("session_id"); Cache::rm("uuid_{$session_id_check}"); Cache::rm("member_info_{$session_id_check}"); Cookie::delete("session_id"); break; case 3:case "redis": break; } $this->member_info = null; $this->uuid = null; return true; } ~~~ * 验证类在控制器中继承和判断使用用户信息的方法 ~~~ <?php /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/3/12 * Time: 11:14 */ namespace app\api\controller; use think\Cache; use think\Debug; //extends继承验证类 class Test extends Auth { public function test(){ //检验用户是否登录 if (!$this->uuid){ return self::showReturnCodeWithOutData(1004); } //使用用户信息 dump($this->member_info); } ~~~ * 控制器登录页面 在登录成功后 设置全局登录 ~~~ /** * 登陆验证 * Power by Mikkle * QQ:776329498 * @return array */ public function login(){ if ($this->request->isAjax()) { //数据库字段 网页字段转换 $param = [ 'username' => 'username', 'password' => 'password', ]; $param_data = $this->buildParam($param); $check_login = $this->doModelAction($param_data, 'base/PersonnelUser.login', 'base/PersonnelUser', 'checkLogin'); //记录错误信息 if (!isset($check_login['code'])) $this->showReturnCodeWithSaveLog(1111); if ($check_login['code'] == 1001) { //设置全局登录 $this->setLoginGlobal($check_login['data'], 1); } return $check_login; }else{ return $this->fetch("login"); } } ~~~ >[warning] 这些源码只是项目摘录并进行修改,我这里也是讲解实现思路和简单的代码示例.希望对大家有所帮助.