11

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...

session_start(): Failed to read session data: user (path: 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.

Then I realised something. See my answer below.

3 Answers 3

46

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!

3
  • 4
    Even thought it says return a string in the manual (php.net/manual/en/sessionhandlerinterface.read.php), I was also returning a null value, you saved me alot of time :)
    – NiGhTHawK
    Jan 23, 2017 at 8:17
  • 2
    It's worth noting that this is also the issue when a boolean is returned (false)
    – pfwd
    Nov 22, 2017 at 14:42
  • Many thanks! I was confused, because PHP says nothing about wrong type of session data. So, now I use (string) casting on return statement in my custom session handler.
    – Fatalist
    Apr 17, 2018 at 14:13
4

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.

2
  • This is a Windows discussion - are you talking Unix or Windows? Feb 9, 2021 at 17:35
  • Linux, though the error I encountered was the same as that of OP (aside from the Linuxy style path spec), and clearly my solution is helping some so does it matter?
    – John Rix
    Feb 9, 2021 at 21:30
-1

After some digging I found that this error in Joomla could be caused by mySQL server not running on my local machine. Starting the mySQL service solved the problem, so it had nothing to do with sessions or permissions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.