简体   繁体   English

实施自动注销

[英]Implementing auto logout

I need a way to auto log out a user after 10 minutes of inactivity. 我需要一种闲置10分钟后自动注销用户的方法。 This includes when they leave the browser. 这包括他们何时离开浏览器。

I use this code for all pages that require log in: 我将此代码用于所有需要登录的页面:

function CheckLogin()
{
     if(!isset($_SESSION))
     { 
     session_start();

     }


     $sessionvar = $this->GetLoginSessionVar();

     if(empty($_SESSION[$sessionvar]))
     {
        return false;
     }
     return true;
}

Also, previous ways I've done it is it would require one to go back to the homepage and then they would be auto logged out. 另外,我以前做过的方法是需要返回首页,然后它们才会自动注销。 Is it possible that they don't need to go back to the page? 他们是否有可能不需要返回页面?

Yes. 是。 session.gc_maxlifetime is exactly what you are looking for: session.gc_maxlifetime正是您要寻找的东西:

ini_set('session.gc_maxlifetime', 600);

http://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime http://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime

There are two ways to go about implementing your timeout. 实现超时有两种方法。 One from the server side and one from the client side. 一种来自服务器端,另一种来自客户端。 Arguably the server side implementation is more important as the client side functions only offer a more enjoyable user experience, and don't actually interact with your application's authentication in anyway. 可以说服务器端的实现更为重要,因为客户端功能只能提供更令人愉悦的用户体验,并且实际上无论如何都不会与应用程序的身份验证进行交互。

Server Side 服务器端

You'll need a login check function which ensures the user is logged in. You've mentioned a psuedo function in your question, so I'll just expand from there. 您将需要一个登录检查功能来确保用户已登录。您在问题中提到了psuedo函数,因此我将在此进行扩展。

 function CheckLogin()
 {
      // Is the user logged in?
      if(session does not exist)
      {
           // redirect
           // redirect or return false
      }
      else
      {
           if(session is valid)
           {
                // user is logged in
                // redirect or return true
           }
           else
           {
                // redirect or return false
           }
      }
 }

Client Side 客户端

You'll need jquery, and the plugin Jim linked to, http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/ . 您将需要jquery,并将插件Jim链接到http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/ Create and link to a Javascript file and include it site wide. 创建并链接到Javascript文件,并将其包含在整个站点中。 Follow the documentation in the link for the plugin. 请遵循插件链接中的文档。 It's fairly thorough. 这是相当彻底的。

It's also in your best interest to set any cookies associated with the session (if there are any) to timeout after 10 minutes. 将与该会话关联的所有cookie(如果有)设置为10分钟后超时也是您的最大利益。

there is also a JQuery plug-in called idle timeout that works great. 还有一个称为空闲超时的JQuery插件,效果很好。 Set two or three parameters, including where to send people when they timeout. 设置两个或三个参数,包括超时时将人员发送到何处。 Here is the link http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/ 这是链接http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM