views:

127

answers:

2

Hello, I've been trying to learn how to use PHP sessions but I'm going around in circles. I understand that a session cookie can be given a lifetime ('session.cookie_lifetime') and that after that lifetime the cookie expires regardless of if a user interacts with the site. I would therefore assume to set this to 0 to indicate they should stay live until the browser closes. I also think I understand that the garbage collection lifetime ('session.gc_maxlifetime') can be set for a cookie and that as long as a user does not exceed this time between their clicks then the cookie will remain active.

To test this out I've been trying to get a 10 second session timeout. I tried: ini_set('session.gc_maxlifetime',10); but the session doesn't timeout after 1 minute at least. ...is this because I am only saying to the garbage collector that the session has a life of 10 seconds but I'm not actually triggering the garbage collector? How do you set the garbage collector going or does it just run everytime a session is requested?

Does anyone have some code that can set the lifetime of cookies that works? I can then go away and work out from it how they work. Thanks

A: 

The server has a default timeout set in it's INI files, if not overridden from within a script. In apache it is set from within PHP.ini i believe. You also need to enable the garbage collection function, which I believe is also set in php.ini.

Kyle J. Dye
+1  A: 

First of all, don't confuse cookie settings (which are client-side) and garbage collection (which is server-side). Cookie settings only affect the expiration of the session_id. Session data may still exist on the server even if the browser has removed the cookie and, on the contrary, the server can remove the data while the session_id is still remembered by the browser.

The cookie can be set to expire when you close the browser or in a specific date and time (I believe the default option is the first one, but I'd have to check it). In both cases, if the user interacts with the site the cookie will remain valid since it's renewed on each response.

Session data is removed when the garbage collection is launched but you must take into account that:

  1. The garbage collection is started randomly, triggered by a page request.

  2. It removes session data not modified in more that gc_maxlifetime seconds.

  3. By default, session data is stored in files and PHP doesn't track what site owns what files. That means that storing sessions in the default shared location makes you lose control on session expiration: the site that's configured to keep session data for the shortest time is likely to remove data from other sites with longer time.

To sum up, if you want full control on your data lifetime you need to store session data in a private directory, e.g.:

<?php

session_save_path('/home/foo/sessions');
ini_set('session.gc_maxlifetime', 3*60*60); // 3 hours
ini_set('session.use_only_cookies', TRUE);
session_start();

?>
Álvaro G. Vicario
Thanks, for the clarification, most helpful, especially the private dir bit, that makes sense. However, I tried the code with a 10 second timeout and it does not log out (after a minute at least).
Columbo
Just tried it at the other end of the scale and set it to 3 hours as in your code. My application has an hour timeout that I create by storing the time in a session variable but the server clears my sessions way before this (probably the default 24 mins). But I used your code and it keeps me logged in. Perhaps there is a minimum setting for gc_maxlifetime.
Columbo
About the 10 second timeout: remember, garbage collector starts randomly! If it runs on average once in 10 seconds, your server is seriously misconfigured ;-)
Álvaro G. Vicario
Good point, thanks for your help, it's working good now.
Columbo