Using PHP, if I set a custom session folder to store session files, what must I do to make sure that old session files eventually get deleted? Is there a way to have Apache or PHP handle this for me, or do I need to set something up to clean this folder out myself? Any information on this subject is greatly appreciated.
I am currently using session_save_path() to change the session folder if that makes a difference.
If you use file driver for session, just remove those files in storage/framework/sessions . In config/session. php you can find lifetime , default it's set for 2 hours. After 2 hours it's automatically removed.
By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier).
gc_probability defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by using gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that the GC process starts on each request. session. gc_divisor defaults to 100 .
session_save_path() returns the path of the current directory used to save session data.
As long as you don't use the N;
option on php's session.save_path setting, PHP will auto-clean stale sessions according to the session.gc_probability / session.gc_divisor / session.max_lifetime settings
If you've rolled your own session handlers, you'll be responsible for the cleanup.
Yes, you need to manually clean them up because you've setup your own session save path. (Today it's said it's for the split directory option only, but I have servers where this is still needed even not using that feature but using a custom session save path and it's some PHP 5.2.x and I need to manually clean.)
You can check the age of a file and delete if it's older than x days/minutes whatever:
cd /path/to/sessions; find -cmin +24 | xargs rm
Taken from the note part of php.ini
:
; NOTE: If you are using the subdirectory option for storing session files
; (see session.save_path above), then garbage collection does *not*
; happen automatically. You will need to do your own garbage
; collection through a shell script, cron entry, or some other method.
; For example, the following script would is the equivalent of
; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
; cd /path/to/sessions; find -cmin +24 | xargs rm
See as well this related/duplicate question: cleanup php session files
"Single" command:
find /path/to/session -name sess_* -cmin +24 -exec rm {} \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With