在 PHP7.1 上读取会话数据失败 [英] Failed to read session data on PHP7.1

查看:63
本文介绍了在 PHP7.1 上读取会话数据失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分享一个我曾经(现在已经解决)的问题.

Sharing a problem that I had (and now solved).

在我的开发机器上,我使用 PHP 运行 IIS.我升级到 PHP7,突然我的代码不再工作,返回这个错误...

On my development machine, I run IIS with PHP. I upgraded to PHP7 and suddenly my code no longer worked, returning this error...

session_start():读取会话数据失败:用户(路径:C:\WINDOWS\temp)

session_start(): Failed to read session data: user (path: C:\WINDOWS\temp)

看起来像是权限问题,对吧?因此,我花了很长时间调整 php.ini 设置并尝试更改文件夹权限 - 没有成功.

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.

然后我意识到了一些事情.请看下面我的回答.

Then I realised something. See my answer below.

推荐答案

我终于意识到消息毫无意义——应用程序使用数据库实现了自己的会话处理程序.在 read 方法中,我从数据库中以字符串形式获取会话数据.

I finally realised the message was meaningless - the application implements its own session handler using the database. In the read method, I get the session data as a string from the database.

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);
    }

如果没有匹配的数据,U_Data::getOneVal 方法有第二个参数要返回.默认值为 null 并且在 PHP5 中运行良好,但在 PHP7.1 中会导致错误.一个简单的改变让它返回一个空字符串来解决这个问题.

The U_Data::getOneVal method has a second parameter to return if there is no matching data. The default is null and 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.

        return U_Data::getOneVal($sql, '');

原来如此.如果您收到有关 session_start 不工作的警告,并且您正在实现自己的会话处理程序,请尝试在 read 方法中检查您的代码,以确保它总是返回一个字符串.

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 read method to make sure it always returns a string.

(注意:U_Data 只是我自己的数据实用程序类)

(Note: U_Data is just my own data utility class)

我希望这能帮别人省下我绞尽脑汁的时间!

I hope this saves someone else the hours I spent racking my brain!

这篇关于在 PHP7.1 上读取会话数据失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆