7

I want to store the created sessions in a directory above the root, except when I use any of the following:

    session_save_path($_SERVER['DOCUMENT_ROOT'] . '/../storage/sessions');
    session_save_path($_SERVER['DOCUMENT_ROOT'] . '/../storage/sessions/'); // With an extra slash at the end
ini_set('session.save_path', $_SERVER['DOCUMENT_ROOT'] . '/../storage/sessions');
ini_set('session.save_path', $_SERVER['DOCUMENT_ROOT'] . '/../storage/sessions/'); // Again with the extra slash

and not one of these methods work, whenever I load the page I get the following errors:

Warning: session_start(): Session data file is not created by your uid in /var/www/bootstrap/bootstrap.php on line 25

Warning: session_start(): Failed to read session data: files (path: /var/www/public/../storage/sessions/) in /var/www/bootstrap/bootstrap.php on line 25

Can anyone help me?

Edit: My server runs on PHP 7.1

Joe
  • 4,877
  • 5
  • 30
  • 51
Oreborous
  • 322
  • 1
  • 3
  • 14
  • 1
    It's a linux user permissions error; make sure your able to read/write to those locations(folders should be 0755 and your files should be 0644), and that your user has permission to read to those locations. – Kaylined Jan 20 '17 at 20:04
  • how can I find out which user wants to access those locations? and changing permissions is chown right? – Oreborous Jan 20 '17 at 20:06
  • I have tried all of the above solutions, change the folders folder, create another folder for the session, manage a session by redis, and nothing worked, which solved my problem leaving session.auto_start = 1 which means enabled, in php.ini – Jerfeson Guerreiro Feb 03 '19 at 14:34

10 Answers10

12

PHP requires session file to be owned by the user running PHP.

Then just run chown -R www-data:www-data /var/www/storage/sessions/ to own session folder and files by your PHP user (www-data:www-data are your PHP user and group separated by :).

You can use PHP method get_current_user() or run in bash php -i | grep user and find your user running PHP there.

zored
  • 2,718
  • 3
  • 19
  • 23
  • in my case `/var/www/storage/sessions/` directory doesn't exist neither `/usr/local/var/www/storage/sessions/` directory exists. and yes i am in mac. also after creating the mentioned directory and trying to change ownership or the directory it shows `chown: www-data: illegal group name` – vaibhav3027 Dec 24 '21 at 10:50
  • Also it used to work before i restarted my mac and now it stopped working – vaibhav3027 Dec 24 '21 at 10:56
4

Editing config/config.yml and replacing save_path:

session:
    save_path: '/tmp'
Ashfaq Muhammad
  • 780
  • 5
  • 10
4

this works for me:

sudo chown -R www-data:www-data /var/lib/php/sessions 
elMatador
  • 51
  • 1
  • 2
  • 2
    Thanks for your effort, but please read the question carefully: OP wants to use a custom session save_path. Your answer might work with the default settings but is unrelated to this question. – Johannes Trümpelmann Jul 19 '18 at 17:49
3

On Ubuntu within Windows 10 , my php user is the default user I create for the first install, I dont know why because the etc/apache2/envvars is not configured like that.

Nevermind, I change it for fix it quickly and now, it works.

export APACHE_RUN_USER=rudak
export APACHE_RUN_GROUP=rudak
rudak
  • 379
  • 3
  • 16
2

What worked for me, could work for you as well.

It looks like something went wrong with the session. Open the browser's DevTools and delete all Sessioncookies.

Florin
  • 5,781
  • 2
  • 20
  • 30
1

I could be that your Session file was created by another user (well UID), try (re)moving the session file(s) from your temp dir something like /var/tmp

Recycled Steel
  • 2,272
  • 3
  • 30
  • 35
1

I use this to also make Named Pipes in php.

<?php
$user = get_current_user();   // get current process user

// We need to modify the permissions so users can write to it

exec( "chown -R {$user}:{$user} $fifoPath" );    
0

Another solution: rename your session folder var/session to var/session_old

create a new folder var/session

and delete the old one.

Martijn van Hoof
  • 740
  • 10
  • 28
0

For lampp users,
- go to /opt/lampp/temp/
- delete session file, and test again

After testing again,
If error message "Wrong permissions on configuration file, should not be world writable!" shows,
Run the following at terminal:
sudo chmod 755 /opt/lampp/phpmyadmin/config.inc.php

That worked for me ...

Felix Htoo
  • 874
  • 12
  • 15
-1

The solution for me was just to wipe the contents of the sessions folder. Not sure how or why that helped, but it did.

Imran Isak
  • 66
  • 1
  • 9