如果用户未处于活动状态,我将如何使PHP会话到期

时间:2014-03-24 06:03:22

标签: javascript php ajax session

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

5 个答案:

答案 0 :(得分:2)

使用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']);

    }

答案 1 :(得分:1)

将上次请求的时间存储在会话中

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

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

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

答案 2 :(得分:1)

客户端解决方案:

您的信息页:

<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');
?>

答案 3 :(得分:0)

我会在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所提到的,将超时更改为您需要的任何时间。在上面的示例中,我将其设置为10000,仅10秒。

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

答案 4 :(得分:0)

// 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>