Tuesday, July 20, 2010

Check session expiration in php

/* The below script will check for users inactivity on page, If use open one page, then store the current time on $_SESSION['start'] variable. If he doesn't move cursor or click any key from keyboard for some time, and then if he click on any link it will check his inactive time. if it is greater than the limited time we set here 5 minutes as exmple, then he will redirected to inactive.php page */
?>
session_start(); // NEVER FORGET TO START
$inactive = 300; //means expire within 5 minutes
if(isset($_SESSION['start']) ) { // if session start set
$session_life = time() - $_SESSION['start'];
if($session_life > $inactive){ //if session time expired
header("Location: inactive.php"); //session expired message page
}
}
$_SESSION['start']=time(); //in pageload current time stored in session.
?>

0 comments: