Put $_SESSION['login_time'] = time();
into the previous authentication page.
And the snipped below in every other page where you want to check the session time-out.
if(time() - $_SESSION['login_time'] >= 1800){
session_destroy(); // destroy session.
header("Location: logout.php");
die(); // See https://thedailywtf.com/articles/WellIntentioned-Destruction
//redirect if the page is inactive for 30 minutes
}
else {
$_SESSION['login_time'] = time();
// update 'login_time' to the last time a page containing this code was accessed.
}
Edit : This only works if you already used the tweaks in other posts, or disabled Garbage Collection, and want to manually check the session duration.
Don't forget to add die()
after a redirect, because some scripts/robots might ignore it. Also, directly destroying the session with session_destroy()
instead of relying on a redirect for that might be a better option, again, in case of a malicious client or a robot.