-1

I have written some code to timeout a session;

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

}

However, the session does not timeout if it's idle for 10 seconds.

fdfdfd
  • 501
  • 1
  • 7
  • 21

1 Answers1

2

Looks like your almost there. I would try this:

<?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();
}
ajtrichards
  • 29,723
  • 13
  • 94
  • 101