会话超时时空闲不工作php


Session time out when idle not working php

我已经编写了一些代码来超时会话;

<?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秒,则不会超时。

看起来你差不多到了。我会试试这个:

<?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();
}