php 在 PHP7.1 上读取会话数据失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41540302/
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
Failed to read session data on PHP7.1
提问by xtempore
Sharing a problem that I had (and now solved).
分享一个我曾经(现在已经解决)的问题。
On my development machine, I run IIS with PHP. I upgraded to PHP7 and suddenly my code no longer worked, returning this error...
在我的开发机器上,我使用 PHP 运行 IIS。我升级到 PHP7,突然我的代码不再工作,返回这个错误......
session_start(): Failed to read session data: user (path: C:\WINDOWS\temp)
session_start():读取会话数据失败:用户(路径:C:\WINDOWS\temp)
It looks like a permissions issue, right? So, I spent a long time tweaking php.ini settings and trying to change folder permissions - with no success.
好像是权限问题吧?因此,我花了很长时间调整 php.ini 设置并尝试更改文件夹权限 - 但没有成功。
Then I realised something. See my answer below.
然后我意识到了一些事情。看我下面的回答。
回答by xtempore
I finally realised the message was meaningless - the application implements its own session handler using the database. In the readmethod, I get the session data as a string from the database.
我终于意识到这条消息毫无意义——应用程序使用数据库实现了自己的会话处理程序。在read方法中,我从数据库中以字符串形式获取会话数据。
class Sess implements SessionHandlerInterface
...
public function read($key)
{
$qKey = U_Data::quote($key);
$dt = U_Data::datetime();
$sql = <<<EOT
SELECT `sess_data` FROM `sess`
WHERE `sess_key` = $qKey
AND `sess_exp_ts` > $dt
ORDER BY `sess_exp_ts` DESC
LIMIT 1
EOT;
return U_Data::getOneVal($sql);
}
The U_Data::getOneValmethod has a second parameter to return if there is no matching data. The default is nulland that worked fine in PHP5, but in PHP7.1 it causes the error. A simple change to have it return an empty string instead solved the problem.
如果没有匹配的数据,U_Data::getOneVal方法有第二个参数要返回。默认值为null,在 PHP5 中运行良好,但在 PHP7.1 中会导致错误。一个简单的改变让它返回一个空字符串,而不是解决了这个问题。
return U_Data::getOneVal($sql, '');
So there it is. If you are getting a warning about session_start not working AND you are implementing your own session handler, try checking your code in the readmethod to make sure it always returns a string.
所以就是这样。如果您收到有关 session_start 不工作的警告并且您正在实现自己的会话处理程序,请尝试检查read方法中的代码以确保它始终返回 string。
(Note: U_Data is just my own data utility class)
(注意:U_Data 只是我自己的数据实用程序类)
I hope this saves someone else the hours I spent racking my brain!
我希望这可以节省别人花费我绞尽脑汁的时间!
回答by John Rix
Was getting the same error myself. After lots of Googling and cursing, it proved that it was indeed a permissions issue in my case, though on my htdocs root folder, rather than on the path mentioned in the error output. The root folder permissions were 700, whereas everything else inside it was 755 (I have a Joomla installation, which prescribes 755 for folder permissions). Fixing the root folder permissions finally unblocked the issue.
我自己也遇到了同样的错误。经过大量的谷歌搜索和诅咒,它证明在我的情况下确实是权限问题,尽管在我的 htdocs 根文件夹中,而不是在错误输出中提到的路径上。根文件夹权限为 700,而其中的其他所有内容均为 755(我有一个 Joomla 安装,规定文件夹权限为 755)。修复根文件夹权限终于解除了这个问题。