我已经编写了一些代码来超时会话;
<?php
session_start();
// set timeout period in seconds
$inactive = 10;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$SESSION_life = time() - $_SESSION['timeout'];
if($SESSION_life > $inactive)
{ session_destroy(); header("Location: login.php");exit; }
}
$_SESSION['timeout'] = time();
if (isset($_SESSION['username'])) {
echo "<center>Welcome </center>" ; // echo "<p> </p>";
echo " <center>". $_SESSION['username']. "</center>" ;
echo "<br /><center>".$_SESSION["role"]."<br /></center>" ;
}else{
header("location:login.php");
}
但是,如果会话空闲10秒,会话不会超时。
答案 0 :(得分:2)
看起来你几乎就在那里。我会试试这个:
<?php
session_start();
$inactive_time = 10;
if(isset($_SESSION['last_active_time'])){
$time_since_last_active = time() - $_SESSION['last_active_time'];
if($time_since_last_active >= $inactive_time){
// The user has been inactive for too long
session_destroy();
header('Location: login.php');
exit;
}else{
// Set the last active tim
$_SESSION['last_active_time'] = time();
}
}else{
$_SESSION['last_active_time'] = time();
}