【问题标题】:PHP - Destroy session if not any action in 10 minutesPHP - 如果在 10 分钟内没有任何操作,则销毁会话
【发布时间】:2012-02-21 10:07:27
【问题描述】:

如果用户在 10 分钟内没有执行任何操作,是否有任何选项可以销毁会话?

【问题讨论】:

    标签: php session session-timeout


    【解决方案1】:
    session_start();
    
    // 10 mins in seconds
    $inactive = 600; 
    
    $session_life = time() - $_SESSION['timeout'];
    
    if($session_life > $inactive) {
       session_destroy();
       header("Location: logoutpage.php");
    }
    
    $_SESSION['timeout']=time();
    

    The code above was taken from this particular page.

    【讨论】:

    • 这是一种 SO (StackOverflow) 风格,直接发布代码然后链接到源代码。如果消息来源关闭您的答案,那么几年后您的答案仍然会有所帮助。但不要忘记链接!
    • @Frankie 请确保您下次正确编辑它。您删除了部分链接,使其指向完全不同的线程!
    【解决方案2】:

    尝试将会话超时设置为 10 分钟。

    ini_set('session.gc_maxlifetime',10);
    

    【讨论】:

    • 我应该把这行代码放在 session.php 的什么地方?
    【解决方案3】:

    我已经用下面的代码完成了:

    //10 min
    if( !isset($_SESSION['logout']) ){
          $_SESSION['logout'] = strtotime('+10 minutes', time()); 
        }
    
        if( time() > $_SESSION['logout'])
        {
          session_destroy();
            header("Location: index.php"); 
        }else{
                $_SESSION['logout'] = strtotime('+10 minutes', time());
            }
        //echo date('Y/m/d h:i:s',$_SESSION['logout']);
        //echo $_SESSION['logout'];
    

    【讨论】:

      【解决方案4】:

      我已经修改了上面的答案,它工作正常:

      // inactive in seconds
      $inactive = 10;
      if( !isset($_SESSION['timeout']) )
      $_SESSION['timeout'] = time() + $inactive; 
      
      $session_life = time() - $_SESSION['timeout'];
      
      if($session_life > $inactive)
      {  session_destroy(); header("Location:index.php");     }
      
      $_SESSION['timeout']=time();
      

      【讨论】:

        【解决方案5】:

        在页面中包含以下 javascript 将通过每秒调用函数 CheckIdleTime() 来检查不活动状态。页面上的活动将_idleSecondsCounter 重置为 0。

        <script type="text/javascript">
            var IDLE_TIMEOUT = 10 * 60;  // 10 minutes of inactivity
            var _idleSecondsCounter = 0;
            document.onclick = function() {
                _idleSecondsCounter = 0;
            };
            document.onmousemove = function() {
                _idleSecondsCounter = 0;
            };
            document.onkeypress = function() {
                _idleSecondsCounter = 0;
            };
            window.setInterval(CheckIdleTime, 1000);
            function CheckIdleTime() {
                _idleSecondsCounter++;
                var oPanel = document.getElementById("SecondsUntilExpire");
                if (oPanel)
                    oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
                if (_idleSecondsCounter >= IDLE_TIMEOUT) {
                    // destroy the session in logout.php 
                    document.location.href = "logout.php";
                }
            }
        </script>
        

        【讨论】:

        • 虽然此代码可以回答问题,但提供有关其解决问题的方式和原因的信息可提高其长期价值。
        【解决方案6】:

        比较两个请求之间的时间戳,一个来自当前请求,一个存储在会话中。

        【讨论】:

          猜你喜欢
          • 2014-09-30
          • 1970-01-01
          • 1970-01-01
          • 2017-09-11
          • 2012-05-25
          • 2011-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多