php 清理php会话文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/654310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cleanup php session files
提问by Hyman
On my website I use PHP sessions. Session information is stored in files in my ./session path. After a few months I discovered that these session files are never deleted, by now there are 145.000 of them in this directory.
在我的网站上,我使用 PHP 会话。会话信息存储在我的 ./session 路径中的文件中。几个月后我发现这些会话文件永远不会被删除,现在这个目录中有 145.000 个。
How should these be cleaned up? Do I have to do it programmatically, or is ther a setting I can use somewhere that would have this cleanup happen automatically?
这些应该怎么清理?我是否必须以编程方式执行此操作,还是可以在某处使用可以自动进行此清理的设置?
EDITforgot to mention: This site runs at a provider, so I don't have access to a command line. I do have ftp-access, but the session files belong to another user (the one the webserver proces runs I guess) From the first answers I got I think it's not just a setting on the server or PHP, so I guess I'll have to implement something for it in PHP, and call that periodically from a browser (maybe from a cron job running on my own machine at home)
编辑忘记提及:此站点在提供商处运行,因此我无权访问命令行。我确实有 ftp 访问权限,但会话文件属于另一个用户(我猜是网络服务器进程运行的那个)从我得到的第一个答案我认为这不仅仅是服务器或 PHP 上的设置,所以我想我会必须在 PHP 中为它实现一些东西,并定期从浏览器调用它(也许从我家里自己的机器上运行的 cron 作业)
采纳答案by Seb
To handle session properly, take a look at http://php.net/manual/en/session.configuration.php.
要正确处理会话,请查看http://php.net/manual/en/session.configuration.php。
There you'll find these variables:
在那里你会发现这些变量:
- session.gc_probability
- session.gc_divisor
- session.gc_maxlifetime
- session.gc_probability
- session.gc_divisor
- session.gc_maxlifetime
These control the garbage collector (GC) probability of running with each page request.
这些控制垃圾收集器 (GC) 与每个页面请求一起运行的概率。
You could set those with ini_set()at the beginning of your script or .htaccess file so you get certainty to some extent they will get deleted sometime.
您可以在脚本或 .htaccess 文件的开头使用ini_set()设置它们,以便在某种程度上确定它们会在某个时候被删除。
回答by Paul Dixon
Debian/Ubuntu handles this with a cronjob defined in /etc/cron.d/php5
Debian/Ubuntu 使用 /etc/cron.d/php5 中定义的 cronjob 处理此问题
# /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 maxlifetime script simply returns the number of minutes a session should be kept alive by checking php.ini, it looks like this
maxlifetime 脚本通过检查 php.ini 简单地返回会话应该保持活动的分钟数,它看起来像这样
#!/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]\+\).*$//p' $ini 2>/dev/null || true);
[ -z "$cur" ] && cur=0
[ "$cur" -gt "$max" ] && max=$cur
done
echo $(($max/60))
exit 0
回答by Andi
In case someone want's to do this with a cronjob, please keep in mind that this:
如果有人想用 cronjob 来做到这一点,请记住:
find .session/ -atime +7 -exec rm {} \;
is really slow, when having a lot of files.
当有很多文件时,真的很慢。
Consider using this instead:
考虑改用这个:
find .session/ -atime +7 | xargs -r rm
In Case you have spaces in you file names use this:
如果文件名中有空格,请使用以下命令:
find .session/ -atime +7 -print0 | xargs -0 -r rm
xargswill fill up the commandline with files to be deleted, then run the rmcommand a lot lesser than -exec rm {} \;, which will call the rmcommand for each file.
xargs将使用要删除的文件填充命令行,然后运行rm比 少得多-exec rm {} \;的rm命令,这将为每个文件调用该命令。
Just my two cents
只是我的两分钱
回答by Daniel Milde
You can create script /etc/cron.hourly/php and put there:
您可以创建脚本 /etc/cron.hourly/php 并将其放在那里:
#!/bin/bash
max=24
tmpdir=/tmp
nice find ${tmpdir} -type f -name 'sess_*' -mmin +${max} -delete
Then make the script executable (chmod +x).
然后使脚本可执行 (chmod +x)。
Now every hour will be deleted all session files with data modified more than 24 minutes ago.
现在每小时将删除所有数据修改时间超过 24 分钟的会话文件。
回答by vartec
Use cron with find to delete files older than given threshold. For example to delete files that haven't been accessed for at least a week.
将 cron 与 find 结合使用以删除早于给定阈值的文件。例如,删除至少一周未访问的文件。
find .session/ -atime +7 -exec rm {} \;
回答by David Lefkon
cd to sessions directory and then:
cd 到会话目录,然后:
1) View sessions older than 40 min:
find . -amin +40 -exec stat -c "%n %y" {} \;
1) 查看超过 40 分钟的会话:
find . -amin +40 -exec stat -c "%n %y" {} \;
2) Remove sessions older than 40 min:
find . -amin +40 -exec rm {} \;
2) 删除超过 40 分钟的会话:
find . -amin +40 -exec rm {} \;
回答by Dr. Tyrell
# 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
The Breakdown:
Only files: 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 matching files: -exec rm -f {} \;
故障:仅文件: find /var/lib/php5/ -type f
超过分钟: -cmin
获取 php 设置: $(echo "`php -i|grep -i session.gc_maxlifetime
做数学: |cut -d ' ' -f3` / 60" | bc)
RM 匹配文件: -exec rm -f {} \;
回答by Frankie
My best guess would be that you are on a shared server and the session files are mixed along all users so you can't, nor you should, delete them. What you can do, if you are worried about scaling and/or your users session privacy, is to move sessions to the database.
我最好的猜测是您在共享服务器上并且会话文件与所有用户混合在一起,因此您不能也不应该删除它们。如果您担心扩展和/或您的用户会话隐私,您可以做的是将会话移动到数据库。
Start writing that Cookie to the database and you've got a long way towards scaling you app across multiple servers when time is due.
开始将该 Cookie 写入数据库,并且在时间到期时跨多个服务器扩展应用程序还有很长的路要走。
Apart from that I would not worry much with the 145.000 files.
除此之外,我不会担心 145.000 个文件。
回答by MaheshPatade
Use below cron:
使用下面的 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

