【问题标题】:How do I expire a PHP session after sometime when user is not active如何在用户不活动的某个时间后使 PHP 会话过期
【发布时间】:2014-05-01 09:07:00
【问题描述】:

如果页面上没有任何活动超过 10 到 20 分钟,我希望 php 会话过期。或者用户超过 20 分钟不可用。假设我们以登录为例,用户登录,如果 20 分钟后没有活动,它应该使会话过期并再次重定向到登录页面。

【问题讨论】:

标签: javascript php ajax session


【解决方案1】:

使用 Jquery

html 或 php 页面:

<body id="homepage" onload="set_interval()" onmousemove="reset_interval()" onclick="reset_interval()" onkeypress="reset_interval()" onscroll="reset_interval()">

jquery

//user login sessions
var timer = 0;
function set_interval() {
  // the interval 'timer' is set as soon as the page loads
  timer = setInterval("auto_logout()", 300000);
  // the figure '10000' above indicates how many milliseconds the timer be set to.
  // Eg: to set it to 5 mins, calculate 5min = 5x60 = 300 sec = 300,000 millisec.
  // So set it to 300000
}
function reset_interval() {
  //resets the timer. The timer is reset on each of the below events:
  // 1. mousemove   2. mouseclick   3. key press 4. scroliing
  //first step: clear the existing timer
  if (timer != 0) {
   clearInterval(timer);
   timer = 0;
   // second step: implement the timer again
   timer = setInterval("auto_logout()", 300000);
   // completed the reset of the timer
  }
}
function auto_logout() {
  // this function will redirect the user to the logout script
  **window.location = "index.php?opt=Logout";**
}

注销页面

if(@$_REQUEST['opt']=='Logout')
    {
        unset($_SESSION['uid']);
        unset($_SESSION['username']);

    }

【讨论】:

  • 如果用户在您的网站上不活跃,它会自动注销并重定向登录页面。
  • 但我在 php 中寻找同样的东西。
【解决方案2】:

在会话中存储最后一次请求的时间

<?php
  $_SESSION['timeout'] = time();
?>

在每个发生的请求中,检查他们在多长时间前提出了上一个请求(本例中为 10 分钟)

<?php
  if ($_SESSION['timeout'] + 10 * 60 < time()) {
     // destroy session & logout
  } else {
     $_SESSION['timeout'] = time();
  }
?>

【讨论】:

  • +1,但 do nothing 实际上应该是 $_SESSION['timeout'] = time();
【解决方案3】:

客户端解决方案:

您的页面:

<script type="text/JavaScript">
    var idleRefresh;
    idleRefresh = setTimeout("location.href = 'unset.php';",30000);
    windows.onmousemove = function() {
        clearTimeOut(idleRefresh);
        idleRefresh = setTimeout("location.href = 'unset.php';",30000);
    };
</script>

unset.php:(取消所有会话变量/特定登录变量,并将用户重定向到登录页面)

<?php
    session_unset();
    header('Location: login.php');
?>

【讨论】:

    【解决方案4】:

    我会在 Arun 的 jquery 解决方案中添加这个添加评论,但还没有 50 名声望。我必须对他的代码进行一些修改才能使其正常工作,例如:

    timer = setInterval("auto_logout()", 300000);
    

    需要改成这样:

    timer = setInterval( function(){ auto_logout() }, 300000);
    

    我在下面包含了完全修改过的代码:

    HTML:

    <body onload="setTimer()" onmousemove="resetTimer()" onclick="resetTimer()" onkeypress="resetTimer()" onscroll="resetTimer()">
    

    而 Javascript 是:

    // Variables: timer, timeout  
    var t, to = 10000;
    
    // Setup the timer
    function setTimer() {
        t = setInterval( function(){
            logout()
        }, to);
    }
    
    // Reset the timer
    function resetTimer() {             
        if (t > 0) {
            clearInterval(t);
            setTimer();
        }
    }
    
    // Logs user out
    function logout() {
        window.location = "login.php";
    }
    

    正如 Arun 所提到的,将 timeout (to) 更改为您需要的任何值。在上面的示例中,我将其设置为 10000,即只有 10 秒。

    希望这会有所帮助 - 再次感谢 Arun 发布原始代码。

    【讨论】:

      【解决方案5】:

      // JQUERY SCRIPT;
      
      var reset = 60000;
      var timer = reset;
      
      $('body').on('click keyup mousemove', function(){
          timer=reset;
      });
      
      // COUNTDOWN FUNCTION 'ct' SHOW THE TIMING ON YOUR SIGNOUT BUTTON AND 
      // WILL REDIRECT TO LOGOUT ACTION AFTER THE REACH SET TIMEOUT;
      function ct(){
          $('#so').text(timer/1000+' Sec. SIGN OUT');
          timer=timer-1000;
          if(timer==0){
              window.location = 'logout.php';
          }
      }
      
      // EXECUTE 'ct' FUNCTION AFTER EVERY 1 SEC.
      setInterval('ct()', 1000);
      /* CSS CODE OF SIGNOUT BUTTON */
      #so{
        background-color: tomato;
        padding:8px 20px;
        color:#fff;
        border-radius: 4px;
        text-decoration: none;
      }
      h3, #so{font-family: Segoe UI;}
      <!-- LIBRARY -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      
      
      <!-- HTML CODE -->
      <a href="logout.php" id="so">SIGN OUT</a>
      <h3>RESET TIMER WITH - MOVE CURSOR, KEY UP &amp; CLICK<h3>

      【讨论】:

        猜你喜欢
        • 2020-10-14
        • 2018-11-24
        • 2018-06-20
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        • 2018-05-25
        • 2015-09-16
        • 1970-01-01
        相关资源
        最近更新 更多