I see there are thousands of files in my "/tmp" directory (a CentOS machine) and almost all of them are PHP session files.
I'm worried about the possible impact this might have on my system.
Are those files ever deleted either by the OS, Apache or PHP? or I have to take care of it myself?

link|improve this question

1  
If this is an application you have developed, then have you considered using database based sessions instead? – Zoredache May 4 '10 at 6:59
@Zoredache, For that I would need to write a session handler which uses mysql. Plus, that would put extra loading on the DB which is already very loaded. I dont know if the impact on performance would be good. – GetFree May 4 '10 at 15:16
feedback

5 Answers

They should be deleted by the PHP garbage collector. The frequency is controlled by the session.gc_maxlifetime setting in php.ini. Possibly if this is not kicking in you have other problems.

link|improve this answer
But garbage collection exits from PHP 5.3 on. What about older versions? – GetFree May 4 '10 at 0:58
If your question is specific to a particular version of PHP, then you need to state this in your question. – dunxd Oct 24 '11 at 10:21
2  
"garbage collection" is used in different ways for different things. The session garbage collection exists since PHP 4.0 (which introduced the session module). What's new in 5.3 is the memory garbage collection for cleaning up cyclic references of PHP variables where the reference counting mechanism keeps them alive till request end. php.net/gc vs. php.net/session.gc-probability – johannes Oct 24 '11 at 11:26
feedback

You could setup a cron script to clean them up automatically. It's generally a good idea to test for creation date older than what the life of cookies is set up to be on your system.

Limiting cookie life is done thusly (must be done before script outputs anything):

<?php
session_name('my_site_name');
session_set_cookie_params(1209600); # max cookie age of 14 days
# send cookie headers
session_start();
?>

Then, in your cleanup script:

#!/bin/sh
find /tmp -type f -maxdepth 1 -name 'php_session_file_prefix*' -ctime +15 -exec rm -f {} \;

Then, in your crontab:

# Run daily cron jobs at 03:40 every day
40 3 * * * /path/to/php-session-cleanup.sh
link|improve this answer
feedback

Also at reboot - as /tmp is always cleared out on reboot.

link|improve this answer
feedback

The PHP cookies get deleted at closing of the browser.But permanent cookies may be created using a database entry or a server side file as a cookie.

link|improve this answer
feedback

This might be an old question, but no one nailed it right to the head: No, by default session files are not deleted at any point of time (at least on a php 5.3 / ubuntu/debian standard set up). php.ini says it like this:

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; http://php.net/session.cookie-lifetime
session.cookie_lifetime = 0

To be honest, i'm am a little bit shocked by this ! I mean, this will cause hundreds of thousands of files ... A browser restart will mean that the user gets a new session id, so the "old one" will never be deleted.

link|improve this answer
That setting has nothing to do with how frequently /tmp is garbage collected. – ceejayoz Apr 6 at 16:12
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.