tags:

views:

187

answers:

2

I periodically remove the tmp out of session files. Apparently, I mismanage sessions because of the huge number of sessions. How do you manage your sessions, not their amount becoming too large? How do you make sure you do not make a new session if one exists?

Command I have used, not sure how they really work:

ob_start (); 
session_save_path("/tmp/");
session_start();
ob_end_flush ();
A: 

PHP should be automatically removing old session files once they're more than a few hours old. Your session.gc_probability, session.gc_divisor and session.gc_maxlifetime settings may contain odd values. Check the sessions runtime configuration section of the PHP manual for more information and their defaults.

This is not something you can (or should) attempt to manage on your own.

If you still end up with too many files, look at using the advanced settings in session.save_path to create subdirectories for the sessions. This should speed up filesystem access to the individual files.

Charles
A: 

In a properly configured PHP installation, you shouldn't need to be cleaning your session data by hand. You should read through the pages on sessions in the manual and check out some of the configuration directives. Odds are that you have set gc_maxlifetime to a very high value, and they simply haven't reached the "expiry" limit to be automatically collected. You should check your applications to be sure that they aren't setting strange values for the session settings (using ini_set()) during runtime.

PHP has an internal algorithm that runs on session_start() that decides whether it should do a garbage cleaning run and remove old session files. For performance reasons, I believe the default chance to do a run each time is 1%, so on average a garbage-collection routine will be started once in every 100 session_start() calls. If you find this isn't enough, you can raise the gc_divisor setting to something higher than 1.

zombat