PHP Session Expire

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.

 

http://septiadi.com/2011/05/19/php-session-expire/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: