End a session after a period of time if the page has been inactive - PHP
I have a website in php which has a log in system. I would like to log the
user out after 10min if the website is inactive. To do this I was going to
use the following code:
session_start();
// set timeout period in seconds
$inactive = 600;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['start'];
if($session_life > $inactive) {
session_destroy();
header("Location: index.php");
}
}
$_SESSION['timeout'] = time();
However when I run the website and refresh the page it logs out and
directs the page to "index.php" even before the "600 seconds" have passed.
What might be the problem here and how can I know that the page has inactive?
Thanks a lot for your help.
No comments:
Post a Comment