<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # 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] 這些源碼只是項目摘錄并進行修改,我這里也是講解實現思路和簡單的代碼示例.希望對大家有所幫助.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看