PHP: Why might session_start fail, claiming "no such file or directory"?

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

Hoss Kash

this is not converting images any more please test it

Michael Gillespie

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

Sister Cynthia

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

Vivian

Hwyd

ADAM

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

nb

why do i have to do a comment

Luis Chavez

No puedo a ser una cuenta nueva

ravi rathod

Please open session

Raj nandani

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

indra

yes l do

CHRISTIAN

GRACIAS AMIGO ME SIRVIO A PELO LA SEGUNDA OPCION!

Aafrin Naaz

I cannot access the page its giving me a session error

chahat soni

It is very nice

David Smith

REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

Mohit bhati

Hi

Shankar Mandal

The session identified is missing

Shankar Mandal

The session identifier is missing

Aman Kumar

The session identifier is missing

Shoaib Shaikh

I can't login

Yong Joh Yan

The session identifier is missing

Aditya Kumar

Regt no - BR/SD/A-20/500258

Harsh

Very bad condition of software or website or may be both

Neeraj Bakmik

I am N C C Cadet

Himanshu Rathore

Yes, USLR will send me a notification.

Himanshu Rathore

I am a NCC cadet.
Software is not working.
Hence, if USLR send me a notification of it.
Then I will join it.

Himanshu Rathore

I am a NCC cadet.
Software is not working.
Hence, if USLR send me a notification of it.
Then I will join it.
##Thank you##

Methusela

I will be greatful if I get opportunity/chance to get your support,I will work hard with any given condition.
my soft ware is not working
help me events to start job
Thank you sir/madam

SURESH p

NCC cadets not working software

Anitha

The session identifier is missing

Lakshmi.R

FAMILY

Lakshmi.R

Smile

Lakshita malik

Bharat internet utsav

Sanjib Boro

Love this

Sanjib Boro

Login problem

Newleaf

This session identifier is missing

Arshika verma

Login problem

Post a comment