5

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.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • possible duplicate of [Session Files Not Getting Cleaned Up](http://stackoverflow.com/questions/6845316/session-files-not-getting-cleaned-up) – hakre Oct 04 '11 at 19:04

3 Answers3

7

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
7

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 {} \;
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Do we need to keep executing this command on regular bases? Also do we need to execute this command on server only or we can execute using PHP file? – vidhi Jun 26 '17 at 06:27
  • @vidhi: This is only needed for specific session configurations which are rare nowadays. If needed, it should be set up in the crontab. If in doubt, contact your sysadmin for the host in question. – hakre Jun 26 '17 at 08:31
0

You will need to periodically clean custom PHP sessions directory. You can run a CRON job to handle the answer by @hakre

We use Plesk and it has a handy CRON/Task manager under Tools & Settings > Scheduled Tasks. You can create a new task and run a script/command at set intervals.

Another point is that it is quite good to run custom session paths if you are on a shared server or the session directory is shared with other sites/resourses.

I'm posting this here as the answer/topic was useful and I've not found much advice online for custom PHP session_path