如何在用户不活动的时候终止PHP会话

时间:2022-07-09 01:17:04

i want php session to be expired if there is no activity on the page for more than 10 to 20 minutes. Or user is not available for more than 20 min.Say we are taking an example of login, user logged in and after 20 min if there is no activity , it should expire the session and redirect to login page again.

如果页面上超过10到20分钟没有活动,我希望php会话过期。或用户不能使用超过20分钟。例如,我们正在使用一个登录的例子,用户登录,如果没有活动,在20分钟后,它应该终止会话并重定向到登录页面。

3 个解决方案

#1


1  

Use Jquery

使用Jquery

html or php page :

html或php页面:

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

jquery

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";**
}

LOGOUT page

注销页面

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

    }

#2


1  

Store the last request made time in session

将最后一个请求存储在会话中

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

In every request happening, check how long ago they made their previous request (10 minutes in this example)

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

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

#3


1  

The Client-side solution:

客户端解决方案:

Your page:

你的页面:

<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: (Unset all session variables / specific login variables, and redirect user to login page)

未设置的。php:(取消设置所有会话变量/特定的登录变量,并将用户重定向到登录页面)

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

#1


1  

Use Jquery

使用Jquery

html or php page :

html或php页面:

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

jquery

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";**
}

LOGOUT page

注销页面

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

    }

#2


1  

Store the last request made time in session

将最后一个请求存储在会话中

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

In every request happening, check how long ago they made their previous request (10 minutes in this example)

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

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

#3


1  

The Client-side solution:

客户端解决方案:

Your page:

你的页面:

<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: (Unset all session variables / specific login variables, and redirect user to login page)

未设置的。php:(取消设置所有会话变量/特定的登录变量,并将用户重定向到登录页面)

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