Clear php session files

I use PHP sessions on my website. Session information is stored in files in my path. / session. After a few months, I found that these session files are never deleted; there are currently 145,000 of them in this directory.

How to clean them? Should I do this programmatically or is it a parameter that I can use somewhere so that this cleanup happens automatically?

EDIT forgot to mention: this site works with the provider, so I do not have access to the command line. I have ftp access, but the session files belong to another user (the one that I believe is starting the web server). From the first answers that I received, I think that this is not just a setup on a server or PHP, so I think that I should implement something for it in PHP and periodically call it from a browser (possibly from a cron job running in my own car at home)

+51
php session
Mar 17 '09 at 13:43
source share
9 answers

To handle the session correctly, see http://php.net/manual/en/session.configuration.php .

You will find the following variables here:

  • session.gc_probability
  • session.gc_divisor
  • session.gc_maxlifetime

They control the likelihood of the garbage collector (GC) running with each page request.

At the beginning of a script or .htaccess file, you can install those with ini_set () so that you get certain certainty they will be deleted someday.

+49
Mar 17 '09 at 14:45
source share

Debian / Ubuntu handles this with the cronjob defined in /etc/cron.d/php5

# /etc/cron.d/php5: crontab fragment for php5 # This purges session files older than X, where X is defined in seconds # as the largest value of session.gc_maxlifetime from all your php.ini # files, or 24 minutes if not defined. See /usr/lib/php5/maxlifetime # Look for and purge old sessions every 30 minutes 09,39 * * * * root [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -r -0 rm 

The maximum time the script simply returns the number of minutes that the session should stay alive by checking php.ini, it looks like

 #!/bin/sh -e max=1440 for ini in /etc/php5/*/php.ini; do cur=$(sed -n -e 's/^[[:space:]]*session.gc_maxlifetime[[:space:]]*=[[:space:]]*\([0-9]\+\).*$/\1/p' $ini 2>/dev/null || true); [ -z "$cur" ] && cur=0 [ "$cur" -gt "$max" ] && max=$cur done echo $(($max/60)) exit 0 
+31
Mar 17 '09 at 13:50
source share

If someone wants to do this with cronjob, please keep in mind that this is:

 find .session/ -atime +7 -exec rm {} \; 

very slow having many files.

Consider instead:

 find .session/ -atime +7 | xargs -r rm 

In case there are spaces in the file names, use this:

 find .session/ -atime +7 -print0 | xargs -0 -r rm 

xargs populate the command line with files to be deleted, and then run the rm command much smaller -exec rm {} \; which will output the rm command for each file.

Only my two cents

+19
Jan 30 '14 at 8:40
source share

Use cron with find to delete files older than a given threshold. For example, to delete files that have not been available for at least a week.

 find .session/ -atime +7 -exec rm {} \; 
+5
Mar 17 '09 at 13:46
source share

You can create a script / etc / cron.hourly / php and put there:

 #!/bin/bash max=24 tmpdir=/tmp nice find ${tmpdir} -type f -name 'sess_*' -mmin +${max} -delete 

Then execute the script executable (chmod + x).

Now every hour all session files with data modified more than 24 minutes ago will be deleted.

+4
May 13 '13 at 10:50
source share
 # Every 30 minutes, not on the hour<br> # Grabs maxlifetime directly from \`php -i\`<br> # doesn't care if /var/lib/php5 exists, errs go to /dev/null<br> 09,39 * * * * find /var/lib/php5/ -type f -cmin +$(echo "\`php -i|grep -i session.gc_maxlifetime|cut -d' ' -f3\` / 60" | bc) -exec rm -f {} \\; >/dev/null 2>&1 

Breakdown: Files only: find / var / lib / php5 / -type f
Older than minutes: -cmin
Get php settings: $ (echo "` php -i | grep -i session.gc_maxlifetime
Do the math: | cut -d '' -f3` / 60 "| bc)
RM Compliance Files: -exec rm -f {} \;

+3
Feb 25 '14 at 0:05
source share

My best guess is that you are on a shared server, and session files are moved with all users, so you cannot and should not delete them. What you can do if you are concerned about the scaling and / or privacy of your user sessions is to move the sessions to the database.

Start writing this cookie to the database, and you have a long way to scale your application on multiple servers when the time comes.

In addition, I would not worry much with 145,000 files.

+2
Mar 18 '09 at 2:44
source share

Use below cron:

 39 20 * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -r -0 rm 
0
Aug 07 '15 at 16:53
source share

cd to the session directory, and then:

1) View sessions older than 40 min: find . -amin +40 -exec stat -c "%n %y" {} \; find . -amin +40 -exec stat -c "%n %y" {} \;

2) Removing sessions older than 40 min: find . -amin +40 -exec rm {} \; find . -amin +40 -exec rm {} \;

0
Mar 30 '17 at 16:31
source share



All Articles