Timed php session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Timed php session

Post by manRay »

I want to make a session where, it logs the user out if their computer has been inactive for (x) amount of time. How can I do this?
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Timed php session

Post by manRay »

I found this on another website, will this work.?

Code: Select all

 
if(time()-$_SESSION['timeStamp']>= 900) { // 900 sec's
unset($_SESSION['login'])//unset all session variables and redirect to Error page "Session TimedOut"
} else {
//update the session['timeStamp'] with current time
$_SESSION['timeStamp'] = time();
}
 
Last edited by Benjamin on Wed May 06, 2009 5:48 pm, edited 1 time in total.
Reason: Added [code=php] tags.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Timed php session

Post by manRay »

I just need it to check if there is any inactivity. I stuff here about forms?
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Timed php session

Post by ldougherty »

Why rely on coding for timing the session? There are settings in your php.ini to manage the session.

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

If you adjust these values or have your host adjust these values for you you can effectively manage how long sessions are active.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Re: Timed php session

Post by mickd »

ldougherty wrote:Why rely on coding for timing the session? There are settings in your php.ini to manage the session.
You might not always want all your sessions to be deleted after a set amount of time. Not all sessions are used with the same lifetime in mind. It's good to have control over it too :)
Post Reply