PHP: how to limit the number of session files?

I periodically remove tmp from session files. Apparently I am frustrating sessions due to the sheer number of sessions. How do you manage your sessions and not their size? How do you make sure you don't create a new session if it exists?

The command I used, I don't know how they work:

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

      

+2


source to share


2 answers


In a properly configured PHP installation, you don't need to manually clear your session data. You should read the pages of sessions in the manual and check out some of the . Most likely you have set gc_maxlifetime to a very high value and they just haven't reached the "expiration" limit that will be automatically compiled. You should check your applications to make sure they are not setting strange session parameter values ​​(using ini_set()

) at runtime.



PHP has an internal algorithm that it runs on session_start()

, which decides if it should do garbage cleanup and delete old session files. For performance reasons, I assume that the default chance of doing a run every time is 1%, so on average the garbage collection routine will run once every 100 calls session_start()

. If you find that this is not enough, you can set gc_divisor to a value greater than 1.

+2


source


PHP should automatically delete old session files when they are more than a few hours old. The session.gc_probability, session.gc_divisor, and session.gc_maxlifetime settings can contain odd values. For more information and defaults, see the Runtime Configuration Session section of the PHP manual .

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



If you still have too many files, take a look at using additional parameters in session.save_path to create subdirectories for sessions. This should speed up the access of the file system to individual files.

0


source







All Articles