PHP 5.3 和会话文件夹的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2904862/
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
Issues with PHP 5.3 and sessions folder
提问by Itay Moav -Malimovka
I recently upgraded to PHP 5.3 and since then I get (sporadic) error messages which indicate Apache (or may be the cleaner of the session files) has no permissions to the folder where the sessions are stored.
This happens randomly and can't be reproduced with exact steps, which led me to guess it is the session cleaner.
Any one has any experience with such errors?
我最近升级到 PHP 5.3,从那时起我收到(零星的)错误消息,表明 Apache(或可能是会话文件的清理者)对存储会话的文件夹没有权限。
这是随机发生的,不能用精确的步骤重现,这让我猜测它是会话清洁器。
任何人都对此类错误有任何经验?
The error message (which is fired on the session_start()line) is:
错误消息(在线上触发session_start())是:
ps_files_cleanup_dir: opendir(/var/lib/php5) failed: Permission denied.
ps_files_cleanup_dir: opendir(/var/lib/php5) 失败:权限被拒绝。
ls -ltr on the session directory gives:
会话目录上的 ls -ltr 给出:
drwx-wx-wt 2 root root 4096 2010-05-25 12:39 php5
Inside this directory I do see session files owned by www-data which is my Apache, and the app does work fine. Which makes me wonder, under which user does the session GC runs?
在这个目录中,我确实看到了 www-data 拥有的会话文件,它是我的 Apache,并且该应用程序运行良好。这让我想知道,会话 GC 在哪个用户下运行?
回答by Diwant Vaidya
The fix:In your php.iniset session.gc_probabilityto 0
解决方法:在您的php.ini设置session.gc_probability中0
The causeI believe I found the answer here http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-session-phpini-garbage
原因我相信我在这里找到了答案http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-session-phpini-garbage
Essentially, the garbage collection is set up to be done by cron jobs on some systems (i.e. Ubuntu/Debian). Some php ini executables like php-cli also try to do garbage collection and that results in the error you got.
本质上,垃圾收集设置为由某些系统(即 Ubuntu/Debian)上的 cron 作业完成。一些 php ini 可执行文件(如 php-cli)也尝试进行垃圾收集,这会导致您遇到错误。
回答by Marie Fischer
This seems to be a typical error on Ubuntu servers (I'm using Lucid LTS). The default permissions of the /var/lib/php5 directory there are
这似乎是 Ubuntu 服务器上的典型错误(我使用的是 Lucid LTS)。/var/lib/php5目录的默认权限有
drwx-wx-wt 2 root root 4096 2011-11-04 02:09 php5
so it can be written but not read by the web server, I guess that explains the errors.
所以它可以被Web服务器写入但不能被读取,我想这可以解释错误。
As Ubuntu has it's own garbage cleaning via cron (/etc/cron.d/php5), it's probably best to disable php's garbage collection as suggested above by Diwant Vaidya.
由于 Ubuntu 通过 cron ( /etc/cron.d/php5)拥有自己的垃圾清理功能,因此最好按照 Diwant Vaidya 上面的建议禁用 php 的垃圾收集。
session.gc_probability = 0
There's actually a reason the session folder should not be world readable - as the PHP Manualsays:
会话文件夹不应该是世界可读的实际上是有原因的 - 正如PHP 手册所说:
If you leave this set to a world-readable directory, such as /tmp (the default), other users on the server may be able to hiHyman sessions by getting the list of files in that directory.
如果将此设置保留为世界可读的目录,例如 /tmp(默认值),服务器上的其他用户可能能够通过获取该目录中的文件列表来劫持会话。
回答by Itay Moav -Malimovka
The solution I currently use (which I am not sure is the correct one) is to give ownership on the session folder to the Apache user (www-data in my case).
我目前使用的解决方案(我不确定是否正确)是将会话文件夹的所有权授予 Apache 用户(在我的情况下为 www-data)。
回答by ChrisFNZ
This issue has been bugging me for a while. I changed the value as suggested in php.ini and the issue kept occurring. I found the same config value in my index.php and also private/Zend/session.php. So it's worth looking a bit deeper if the issue keeps occurring. I hope this is useful for someone.
这个问题已经困扰我一段时间了。我按照 php.ini 中的建议更改了值,但问题一直在发生。我在 index.php 和 private/Zend/session.php 中发现了相同的配置值。因此,如果问题不断发生,则值得深入研究。我希望这对某人有用。

