Blog > detail

Just my tiny mind

PHP Session Expire

26-2-2012 Admin General 2 Comments

PHP Session Expire

Previously, I just search through the internet to find PHP Session Expire. However, I couldn't find what I need.


In this case, I just want to make my system will expire the session if the user dont have activity on the page. But, I havent found PHP function to check the status of user, because PHP is server side. Despite of checking the user activity on the page, I rather choose to expire the session if the user not refreshing the page for some time.


Here is some code to expire the session if the user dont refresh the page.


session_start();

$timeout = 300;//in second

if(isset($_SESSION['time_stamp']) ) {

$idle_time = time() - $_SESSION['time_stamp'];

if($idle_time > $timeout){

session_destroy();

echo "Session Destroyed";

//header('Location:logout.php');

//some code when session is expired

}

else{

$_SESSION['time_stamp'] = time();

//refreshing time stamp

}

}

else{

$_SESSION['time_stamp'] = time();

//make time stamp

}


Just to keep in mind PHP Session Expire script have to be placed on the top of every page. Hope this PHP Session Expire can help you alot.


 PHP Session Expire

PHP Session Expire

word word

PHP Session Expire

PHP Session Expire

PHP Session Expire