<small id='IH0Ks'></small><noframes id='IH0Ks'>

<tfoot id='IH0Ks'></tfoot>
  • <legend id='IH0Ks'><style id='IH0Ks'><dir id='IH0Ks'><q id='IH0Ks'></q></dir></style></legend>
    • <bdo id='IH0Ks'></bdo><ul id='IH0Ks'></ul>
  • <i id='IH0Ks'><tr id='IH0Ks'><dt id='IH0Ks'><q id='IH0Ks'><span id='IH0Ks'><b id='IH0Ks'><form id='IH0Ks'><ins id='IH0Ks'></ins><ul id='IH0Ks'></ul><sub id='IH0Ks'></sub></form><legend id='IH0Ks'></legend><bdo id='IH0Ks'><pre id='IH0Ks'><center id='IH0Ks'></center></pre></bdo></b><th id='IH0Ks'></th></span></q></dt></tr></i><div id='IH0Ks'><tfoot id='IH0Ks'></tfoot><dl id='IH0Ks'><fieldset id='IH0Ks'></fieldset></dl></div>

      1. Laravel 5.2 - 如何从用户的所有设备上注销

        时间:2023-10-04

        • <bdo id='8TXI7'></bdo><ul id='8TXI7'></ul>
            <i id='8TXI7'><tr id='8TXI7'><dt id='8TXI7'><q id='8TXI7'><span id='8TXI7'><b id='8TXI7'><form id='8TXI7'><ins id='8TXI7'></ins><ul id='8TXI7'></ul><sub id='8TXI7'></sub></form><legend id='8TXI7'></legend><bdo id='8TXI7'><pre id='8TXI7'><center id='8TXI7'></center></pre></bdo></b><th id='8TXI7'></th></span></q></dt></tr></i><div id='8TXI7'><tfoot id='8TXI7'></tfoot><dl id='8TXI7'><fieldset id='8TXI7'></fieldset></dl></div>

          • <small id='8TXI7'></small><noframes id='8TXI7'>

            <legend id='8TXI7'><style id='8TXI7'><dir id='8TXI7'><q id='8TXI7'></q></dir></style></legend>
            <tfoot id='8TXI7'></tfoot>

                <tbody id='8TXI7'></tbody>

                1. 本文介绍了Laravel 5.2 - 如何从用户的所有设备上注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当用户从特定设备注销时,我想从他迄今为止登录的所有设备注销.我如何在 Laravel 中做到这一点.

                  When a user logged out from a perticular device I want to logout from all the device he has logged in till now . How I do it in Laravel.

                  我使用Redis通过安装predis/predis":~1.0"

                  I have used Redis for keeping the userId in Session by installing "predis/predis": "~1.0"

                  这是我的登录和注销控制器:

                  And Here is my controller for SignIn and Logout:

                    public function postSignIn(Request $request)
                      {       
                  
                         if (Auth::attempt(['email' => $request['email'], 'password' =>$request['password'] ]) ) {
                  
                         $redis = Redis::connection();   
                          $userId=Session::getId();
                          $redis->sadd('users:sessions:'.$userId,Session::getId());
                            return redirect()->route('main');
                  
                          }
                          return redirect()->back();
                      }
                  
                  
                  
                  public function getLogout()
                  {
                      $redis = Redis::connection();
                      $userId=Session::getId();
                      $userSessions = $redis->smembers('user:sessions:' . $userId);
                      $currentSession = Session::getId();
                      foreach ($userSessions as $sessionId) {
                           if ($currentSession == $sessionId) {
                        continue; 
                  
                              }
                               $redis->srem('user:sessions:' . $userId, $sessionId);
                              $redis->del('laravel:' . $sessionId);
                  
                          }
                      Auth::logout();
                      return redirect()->route('main');
                  }
                  

                  它已成功登录并注销,但不会终止其他设备中的所有会话.

                  It's successfully get logged in and also logged out but it doesn't kill all the session in other devices.

                  我该如何解决问题?

                  推荐答案

                  所以问题是 redis 键名输入错误,用于写入数据$redis->sadd('users:sessions:'.$userId,Session::getId());其中键的前缀 'users:sessions:' 和用于获取数据$redis->srem('user:sessions:' . $userId, $sessionId); where key 的前缀 'user:sessions:'这就是代码不起作用并且 dd() 返回空数组的原因.

                  So issue was with typo in redis key name, for write data used $redis->sadd('users:sessions:'.$userId,Session::getId()); where key's prefix 'users:sessions:' and for get data used $redis->srem('user:sessions:' . $userId, $sessionId); where key's prefix 'user:sessions:' Thats why code didn't work and dd() returned empty array.

                  正确的代码看起来像这样

                  so correct code looks like this

                  public function postSignIn(Request $request)
                  {    
                  
                     if (Auth::attempt(['email' => $request['email'], 'password' =>$request['password'] ]) ) {
                          $redis = Redis::connection();   
                          $userId=Session::getId();
                          $redis->sadd('user:sessions:'.$userId,Session::getId());
                          return redirect()->route('main');
                      }
                      return redirect()->back();
                  }
                  
                  
                  
                  public function getLogout()
                  {
                      $redis = Redis::connection();
                      $userId=Session::getId();
                      $userSessions = $redis->smembers('user:sessions:' . $userId);
                      $currentSession = Session::getId();
                  
                      foreach ($userSessions as $sessionId) {
                           if ($currentSession == $sessionId) {
                               continue; 
                           }
                              $redis->srem('user:sessions:' . $userId, $sessionId);
                              $redis->del('laravel:' . $sessionId);
                          }
                      Auth::logout();
                      return redirect()->route('main');
                  }
                  

                  这篇关于Laravel 5.2 - 如何从用户的所有设备上注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 缓存与 Redis 非常慢 下一篇:你如何搜索带有值的键?例如,获取值为“someva

                  相关文章

                  最新文章

                    <tfoot id='fxUe4'></tfoot>
                    <i id='fxUe4'><tr id='fxUe4'><dt id='fxUe4'><q id='fxUe4'><span id='fxUe4'><b id='fxUe4'><form id='fxUe4'><ins id='fxUe4'></ins><ul id='fxUe4'></ul><sub id='fxUe4'></sub></form><legend id='fxUe4'></legend><bdo id='fxUe4'><pre id='fxUe4'><center id='fxUe4'></center></pre></bdo></b><th id='fxUe4'></th></span></q></dt></tr></i><div id='fxUe4'><tfoot id='fxUe4'></tfoot><dl id='fxUe4'><fieldset id='fxUe4'></fieldset></dl></div>
                    • <bdo id='fxUe4'></bdo><ul id='fxUe4'></ul>

                      <small id='fxUe4'></small><noframes id='fxUe4'>

                      <legend id='fxUe4'><style id='fxUe4'><dir id='fxUe4'><q id='fxUe4'></q></dir></style></legend>