You may also apply the same concept explained by Jessica when user login insert a time from time(); function in the database.
And when you check for user login or not put this code of script.
$inactive = 300; // for 5 minute
$current_time = time();
$prev_time = /*select prev time from the database*/
$session_life = $current_time - $prev_time;
if($session_life > $inactive)
{
unset($_SESSION[$user]);
}
else
{
$sql="update $table set $field_name = $current_time";
}
i hope this will be helpfull for you