php 调用 session_start() 时如何修复权限错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4585086/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:34:21  来源:igfitidea点击:

How can I fix the Permission error when I call session_start()?

phpsessionpermissionswarnings

提问by Waseem Abu Senjer

when I uploaded the script to the server I got this error

当我将脚本上传到服务器时出现此错误

Warning: Unknown: open(/tmp/sess_58f54ee6a828f04116c2ed97664497b2, 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 (/tmp) in Unknown on line 0

警告:未知:打开(/tmp/sess_58f54ee6a828f04116c2ed97664497b2,O_RDWR)失败:第0行未知中的权限被拒绝(13)

警告:未知:无法写入会话数据(文件)。请验证 session.save_path 的当前设置在第 0 行的 Unknown 中是否正确 (/tmp)

the error appeared when I call session_start();although I set the permission of /tmp folder to 777 .

session_start();尽管我将 /tmp 文件夹的权限设置为 777 ,但当我调用时出现错误。

回答by Tadas ?ubonis

Change session path where you can write data or contact server administrator about /tmp problem

更改会话路径,您可以在其中写入数据或就 /tmp 问题联系服务器管理员

http://php.net/manual/en/function.session-save-path.php

http://php.net/manual/en/function.session-save-path.php

回答by German Rumm

you will need to change your session.save_pathphp.ini directive

你需要改变你的session.save_pathphp.ini 指令

You can do that using session_save_path

你可以使用 session_save_path

回答by pixeline

If you have SSH access, here is how to correct the permission and ownership

如果您有 SSH 访问权限,以下是更正权限和所有权的方法

sudo chown -R NAME_OF_USER /tmp

Replace NAME_OF_USER by the user under which runs php. You can find it by simply putting these lines in a php file:

将 NAME_OF_USER 替换为运行 php 的用户。您只需将这些行放在一个 php 文件中即可找到它:

$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];
exit;

回答by Norm

Additionally, you may want to use ini_set('session.save_path', '/dir/here');assuming you have access to this function. The other ways suggested are valid.

此外,ini_set('session.save_path', '/dir/here');假设您有权访问此功能,您可能希望使用。建议的其他方式是有效的。

回答by Haz

Check that you're not running into diskspace issues. If all the permissions are correct (and 777 ought to do it for you), then you might still get this error (for some versions of PHP and Apache) if there isn't enough space to write to the disk.

检查您没有遇到磁盘空间问题。如果所有权限都正确(并且 777 应该为您完成),那么如果没有足够的空间来写入磁盘,您可能仍然会收到此错误(对于某些版本的 PHP 和 Apache)。

回答by Ozan Olmez

Add following line

添加以下行

ini_set('session.save_path', getcwd() . '/tmp');

before

session_start(); 

回答by Whipenstein

I realize that this is an old post, however I just ran into this problem, and found an easy solution.

我意识到这是一个旧帖子,但是我刚刚遇到了这个问题,并找到了一个简单的解决方案。

For me, the issue was happening with one of my websites deployed locally. I hadn't tried accessing the websites using other browsers, but it was happening every time I tried to access this site via Chrome. I decided to go into the Chrome developer tools, under the application tab -- and clicking "Clear Storage". Voila -- everything is working like magic again.

对我来说,问题发生在我本地部署的网站之一。我没有尝试使用其他浏览器访问这些网站,但是每次我尝试通过 Chrome 访问该网站时都会发生这种情况。我决定进入 Chrome 开发人员工具,在应用程序选项卡下——然后单击“清除存储”。瞧——一切都像魔术一样运作。

Hope this helps someone else!

希望这对其他人有帮助!

回答by oussaka

I have the same problem of permission, but on /var/lib/php/session/.

我有同样的权限问题,但在 /var/lib/php/session/ 上。

To fix it, I delete the file and restart php-fpm.

为了修复它,我删除了该文件并重新启动 php-fpm。

rm -rf /var/lib/php/session/sess_p930fh0ejjkeeiaes3l4395q96
sudo service php5.6-fpm restart

Now all works well.

现在一切正常。

回答by TheKLF99

I've just had exactly the same problem with one of my PHP scripts and I was like what did I break 'cos it worked perfectly the day before and I'm running it from my own local Puppy Linux machine so it's not even a host or anything.

我的一个 PHP 脚本刚刚遇到了完全相同的问题,我就像我破坏了什么一样,因为它在前一天工作得很好,而且我是从我自己的本地 Puppy Linux 机器上运行它的,所以它甚至不是主机或任何东西。

The only thing I'd been doing before that was trying to get Java to work in the web browser, so some how I'd managed to get Java to work but broke PHP - oops!

在那之前我唯一要做的就是让 Java 在网络浏览器中工作,所以我设法让 Java 工作但破坏了 PHP - 哎呀!

Anyway I did remember that whilst trying to get Java to work I had deleted the contents of the /tmp folder to wipe anything out that may be causing a problem (it actually turned out with Java I was using the old plugin oij with the new Firefox)

无论如何,我确实记得在尝试让 Java 工作时,我删除了 /tmp 文件夹的内容以清除可能导致问题的任何内容(实际上我在 Java 中使用旧插件 oij 和新的 Firefox )

To solve this problem I opened up Rox File Manager, went to the / folder and right clicked on the tmp folder -> Mount Point 'tmp' and clicked properties.

为了解决这个问题,我打开了 Rox 文件管理器,转到 / 文件夹并右键单击 tmp folder -> Mount Point 'tmp' and clicked properties

I noticed the permissions were set as Owner - Read, Write, Exec, but Group and World were only set at Read and Exec and not Write. I put a tick in Write for both Group and World and now PHP works fine again.

我注意到权限设置为所有者 - 读取、写入、执行,但组和世界仅设置为读取和执行而不是写入。我为 Group 和 World 都勾选了 Write,现在 PHP 又可以正常工作了。

I don't know at what point the permissions for tmp must have changed but for PHP to use them it must be set to have Write permissions.

我不知道在什么时候 tmp 的权限必须改变,但 PHP 要使用它们必须设置为具有写入权限。

回答by Erwin Okken

I had this problem in the following situation:

我在以下情况下遇到了这个问题:

  1. I filled some session vars with PHP
  2. While the session was still active, I changed from PHP 5.4 to 5.3 on my host.
  3. Reloading the page gave the error, described above.
  4. Reset the PHP version to 5.4 again.
  5. Used session_unset(); and session_destroy(); to clean the current session.
  6. Changed the PHP version back to 5.3.
  7. Now it works again.
  1. 我用 PHP 填充了一些会话变量
  2. 当会话仍然处于活动状态时,我在主机上从 PHP 5.4 更改为 5.3。
  3. 重新加载页面会出现上述错误。
  4. 再次将 PHP 版本重置为 5.4。
  5. 使用 session_unset(); 和 session_destroy(); 清理当前会话。
  6. 将 PHP 版本改回 5.3。
  7. 现在它又工作了。

Conclusion: For an irrelevant reason I had to change my PHP version, and while switching with sessions alive, the sessions get corrupted.

结论:出于一个无关紧要的原因,我不得不更改我的 PHP 版本,并且在切换活动会话时,会话已损坏。