Sunday, April 14, 2013

PHP session timeout

We can add this simple code to create a session time out when no activity for a certain duration. 

<?php
$duration=900; //set how long before timeout in seconds
//If it is longer than duration set, time out occurerd and session will be destroyed
if(time() - $_SESSION['LAST_ACTIVITY'] > $duration)
{
session_unset();
session_destroy();
exit;
}
else //If last activity is within 900s, refresh the last activity time
{
$_SESSION['LAST_ACTIVITY'] = time();
}
?>

No comments:

Post a Comment