Failed to read session data on PHP7.1

天涯浪子 提交于 2019-12-03 08:16:31

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

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, '');

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!