PHP 会话处理错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5104065/
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
PHP session handling errors
提问by g2avityhitz
I have this at the very top of my send.php
file:
我在send.php
文件的最顶部有这个:
ob_start();
@session_start();
//some display stuff
$_SESSION['id'] = $id; //$id has a value
header('location: test.php');
And the following at the very top of my test.php file:
在我的 test.php 文件最顶部的以下内容:
ob_start();
@session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
print_r($_SESSION);
When the data sends to test.php
, the following is displayed:
当数据发送到 时test.php
,显示如下:
Array ( )
Warning: Unknown: open(/var/lib/php/session/sess_isu2r2bqudeosqvpoo8a67oj02, O_RDWR) failed: Permission denied (13) in Unknown on line 0
Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0
Array ( )
警告:未知:打开(/var/lib/php/session/sess_isu2r2bqudeosqvpoo8a67oj02,O_RDWR)失败:第0行未知中的权限被拒绝(13)
警告:未知:无法写入会话数据(文件)。请验证 session.save_path 的当前设置是否正确 (/var/lib/php/session) in Unknown on line 0
I've tried only using session_start(); but the results are the same.
我只试过使用 session_start(); 但结果是一样的。
回答by kn3l
Look at your message
看你的留言
So firstthing it relate to permission
所以第一件事与许可有关
open(/var/lib/php/session/sess_isu2r2bqudeosqvpoo8a67oj02, O_RDWR) failed: Permission denied (13) in Unknown on line 0
打开(/var/lib/php/session/sess_isu2r2bqudeosqvpoo8a67oj02,O_RDWR)失败:第0行未知中的权限被拒绝(13)
you have to check file permission
change mode this /var/lib/php/session/
你必须检查文件权限更改模式 /var/lib/php/session/
Second thingit relate to session.save_path
它涉及的第二件事session.save_path
Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0
警告:未知:无法写入会话数据(文件)。请验证 session.save_path 的当前设置是否正确 (/var/lib/php/session) in Unknown on line 0
in php.ini
在 php.ini
[Session]
; Handler used to store/retrieve data.
session.save_handler = files
; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
;
; As of PHP 4.0.1, you can define the path as:
;
; session.save_path = "N;/path"
;
; where N is an integer. Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories. This is useful if you
; or your OS have problems with lots of files in one directory, and is
; a more efficient layout for servers that handle lots of sessions.
;
; NOTE 1: PHP will not create this directory structure automatically.
; You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
; use subdirectories for session storage
;
session.save_path = /tmp/ <= HERE YOU HAVE TO MAKE SURE
; Whether to use cookies.
session.use_cookies = 1
回答by Your Common Sense
you have to change your session.save_path
setting to the accessible dir, /tmp/
for example
你有你的更改session.save_path
设置的访问目录,/tmp/
例如
How to change: http://php.net/session_save_path
如何更改:http: //php.net/session_save_path
Being on the shared host, it is advised to set your session save path inside of your home directory but below document root
在共享主机上,建议将会话保存路径设置在主目录内,但在文档根目录下
also note that
还要注意的是
- using ob_start is unnecessary here,
- and I am sure you put @ operator by accident and already going to remove it forever, don't you?
- 在这里不需要使用 ob_start,
- 而且我确定您不小心放置了@ 运算符并且已经将其永久删除,不是吗?
回答by roberthuttinger
This was a known bugin version(s) of PHP . Depending on your server environment, you can try setting the sessions folder to 777:
这是PHP 版本中的一个已知错误。根据您的服务器环境,您可以尝试将会话文件夹设置为 777:
/var/lib/php/session
(your location may vary)
/var/lib/php/session
(您的位置可能会有所不同)
I ended up using this workaround:
我最终使用了这个解决方法:
session_save_path('/path/not/accessable_to_world/sessions');
ini_set('session.gc_probability', 1);
You will have to create this folder and make it writeable. I havent messed around with the permissions much, but 777 worked for me (obviously).
您必须创建此文件夹并使其可写。我没有过多地处理权限,但是 777 对我有用(显然)。
Make sure the place where you are storing your sessions isn't accessible to the world.
确保世界无法访问您存储会话的位置。
This solution may not work for everyone, but I hope it helps some people!
此解决方案可能不适用于所有人,但我希望它可以帮助某些人!
回答by siveict.co.za
Go to your PHP.ini
file or find PHP.ini EZConfig
on your Cpanel
and set your session.save_path
to the full path leading to the tmp
file, i.e: /home/cpanelusername/tmp
转到您的PHP.ini
文件或PHP.ini EZConfig
在您的文件上找到Cpanel
并将您设置session.save_path
为通向该tmp
文件的完整路径,即:/home/cpanelusername/tmp
回答by Dan Bray
You can fix the issue with the following steps:
您可以通过以下步骤解决问题:
- Verify the folder exists with
sudo cd /var/lib/php/session
. If it does not exist thensudo mkdir /var/lib/php/session
or double check the logs to make sure you have the correct path. - Give the folder full permissions with
sudo chmod 777 /var/lib/php/session
.
- 验证文件夹是否存在
sudo cd /var/lib/php/session
。如果它不存在,则sudo mkdir /var/lib/php/session
或仔细检查日志以确保您拥有正确的路径。 - 使用
sudo chmod 777 /var/lib/php/session
.
Rerun you script and it should be working fine, however, it's not recommended to leave the folder with full permissions. For security, files and folders should only have the minimum permissions required. The following steps will fix that:
重新运行您的脚本,它应该可以正常工作,但是,不建议让该文件夹具有完全权限。为了安全起见,文件和文件夹应仅具有所需的最低权限。以下步骤将解决这个问题:
- You should already be in the session folder so just run
sudo ls -l
to find out the owner of the session file. - Set the correct owner of the session folder with
sudo chown user /var/lib/php/session
. - Give just the owner full read, write, and execute permissions with
sudo chmod 700 /var/lib/php/session
.
- 您应该已经在会话文件夹中,因此只需运行
sudo ls -l
即可找出会话文件的所有者。 - 使用 设置会话文件夹的正确所有者
sudo chown user /var/lib/php/session
。 - 仅授予所有者完整的读取、写入和执行权限
sudo chmod 700 /var/lib/php/session
。
NB
NB
You might not need to use the sudo
command.
您可能不需要使用该sudo
命令。
回答by Iggy
When using latest WHM (v66.0.23) you may go to MultiPHP INI Editor choose PHP version and set session.save_path to default i.e. /var/cpanel/php/sessions/ea-php70 instead of previous simple tmp - this helped me to get rid of such errors.
使用最新的 WHM (v66.0.23) 时,您可以转到 MultiPHP INI Editor 选择 PHP 版本并将 session.save_path 设置为默认值,即 /var/cpanel/php/sessions/ea-php70 而不是以前的简单 tmp - 这帮助我得到摆脱这样的错误。
回答by Daniel Kutik
please make sure the session.save_path
is set correctly in the php.ini
. php needs read/write access to the directory to which this variable is set.
请确保session.save_path
在php.ini
. php 需要对设置此变量的目录进行读/写访问。
more information: http://www.php.net/manual/en/session.configuration.php#ini.session.save-path
更多信息:http: //www.php.net/manual/en/session.configuration.php#ini.session.save-path
回答by Danny van der Knaap
I had the same error everything was correct like the setting the folder permissions.
我有同样的错误,一切都是正确的,就像设置文件夹权限一样。
It looks like an bug in php in my case because when i delete my PHPSESSID cookie it was working again so aperently something was messed up and the session got removed but the cookie was still active so php had to define the cause differently and checking first if the session file is still they and give another error and not the permission error
就我而言,它看起来像是 php 中的一个错误,因为当我删除我的 PHPSESSID cookie 时,它又在工作了,所以显然有些东西搞砸了,会话被删除了,但 cookie 仍然处于活动状态,因此 php 必须以不同的方式定义原因并首先检查是否会话文件仍然是他们并给出另一个错误而不是权限错误
回答by Fizzy Chan
I'm using php-5.4.45 and I got the same problem.
我正在使用 php-5.4.45 并且遇到了同样的问题。
If you are a php-fpm user, try edit php-fpm.conf and change listen.owner and listen.group to the right one. My nginx user is apache, so here I change these to params to apache, then it works well for me.
如果您是 php-fpm 用户,请尝试编辑 php-fpm.conf 并将 listen.owner 和 listen.group 更改为正确的。我的 nginx 用户是 apache,所以在这里我将这些参数更改为 apache,然后它对我来说效果很好。
For apache user, I guess you should edit your fast-cgi params refer the two params I mention above.
对于 apache 用户,我想你应该编辑你的 fast-cgi 参数,参考我上面提到的两个参数。
回答by Chetabahana
If you use a configured vhost and find the same error then you can override the default setting of php_value session.save_path
under your <VirtualHost *:80>
如果使用配置的虚拟主机,并发现了同样的错误,那么你可以重写的默认设置php_value session.save_path
下您的<VirtualHost *:80>
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/5.6/session"
php_value soap.wsdl_cache_dir "/var/lib/php/5.6/wsdlcache"
Change the path to your own '/tmp'
with chmod 777.
'/tmp'
使用 chmod 777将路径更改为您自己的路径 。