2014-05-09: PHP: Why might session_start fail, claiming "no such file or directory"?

Posted at 2014-05-09 15:49:30 by SHD

Here's a little problem that bit me after an OS upgrade to a webserver. Whenever session_start was called, PHP would throw a warning message and not actually start the session:

Warning: session_start() [function.session-start]: open(/tmp/php_sessions/sess_5q2snk41jrvq9tt1q8eddprlulm7jaflk0hgtfivm8uhqr2i85u0, O_RDWR) failed: No such file or directory (2) in /srv/www/htdocs/index.php on line 2
Warning: Unknown: open(/tmp/sess_5q2snk41jrvq9tt1q8eddprlulm7jaflk0hgtfivm8uhqr2i85u0, O_RDWR) failed: No such file or directory (2) 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/php_sessions) in Unknown on line 0

While this normally means that the directory specified as PHP's session.save_path either does not exist or does not have the appropriate permissions set, I could see quite clearly that that directory in fact did exist and was owned by the appropriate user, with the correct permissions. It took me hours to find out what was wrong. It turns out that the OS upgrade made systemd redirect Apache's (and thus PHP's) references to /tmp to another location, something like /tmp/systemd-private-RiMSxa.

Solution #1: Create the directory

There's multiple solutions to this. One would be to simply create the appropriate directory as needed, as simple as:

<?php
mkdir 
(ini_get ('session.save_path'0777true));
?>

Solution #2: Move your sessions elsewhere

A second solution is to simply change your php.ini file, making the session.save_path directive point somewhere outside the /tmp directory, such as /var/lib/php.

Solution #3: Disable private temporary directory

And another would be to disable the redirection (which is what I did, as this is a development server where I sometimes find it useful to inspect the session files manually. For this, find the appropriate configuration file for systemd. For me, this was /usr/lib/systemd/system/apache2.service (the file could also be called httpd.service). In that, find the line:

PrivateTmp=true

and change true to false. Save the file, call systemctl daemon-reload and restart Apache.

Comments

this is not converting images any more please test it

Posted at 2015-04-14 00:45:57 by Hoss Kash

Thanks, this article helped me figure out what was going on with my particular sessions case. This was on my localhost, and the baffling thing was that multiple other sites on my localhost were not affected. The one thing that was different with this virtual host was I had it's directory mapped to a network drive instead of a local directory - since this is a domain-authenticated network drive, Apache's user could not access it for reading. It was generating the session variable under the normal tmp/ directory, but I guess reading was getting redirected then. I couldn't find the PrivateTmp line in any config files (XAMPP for Windows), so I ended up moving the site to my localhost.

Once again, thanks for thanks for the information. This was far more helpful then every other resource I found of just "check your session.save_path"...

Posted at 2015-06-10 01:23:40 by Michael Gillespie

I want to register and I think perhaps I need troobleshooting!!!

Posted at 2016-11-22 00:44:13 by Sister Cynthia

Hwyd

Posted at 2016-12-07 10:54:47 by Vivian

fuuuuck you !!!!!!why i cant login using my facebook account???this is my account not your account!!!so i can do anything!!!!!!!!!!!!!!

Posted at 2017-05-07 13:32:10 by ADAM

why do i have to do a comment

Posted at 2017-07-09 01:41:25 by nb

Post a comment

Note: HTML is not permitted, URLs will be linked automatically. Spam comments will result in a permanent ban.
Type these 4 symbols into the edit field