2

There's a lot of questions on here about PHP sessions, but none of the solutions seem to be working for me. I made a test script to showcase the issue:

session1.php

session_start();

$_SESSION['user'] = 'Test';

header('location: session2.php');
exit;
?>

session2.php

session_start();

print_r($_SESSION);

?>

This is a very simple example, however I'm using sessions in my site to rely on if a user is logged in or not. It worked for a while, then I had to reinstall my server and it just stopped working. For reference, this is the question that I used which had a lot of possible solutions in it: Session variables not working php

Here's what I've tried so far

  • Put exit; after the header redirect
  • Redirect to the same domain, I've tried putting the entire url in the redirect as well but it was not successful. Besides, it should do this automatically if you're just putting in a file name (from my experience)
  • Setting the session.save_path to "/var/lib/php/session" in php.ini
  • Ensuring full read/write access for the script owner (root), also making sure root is the owner ls -ld /var/lib/php/session returns

drwxrwxrwx. 2 root apache 4096 Apr 9 16:55 /var/lib/php/session

  • Setting session.cookie_lifetime to 3600, shouldn't be necessary but I can just try
  • Changing session.cookie_domain to my domain, again, it shouldn't be necessary since it does this properly by default

None of this worked for me so far. I hope I'm overlooking something simple.

As you can see, the $_SESSION is entirely empty in session2.php. Btw, I have rebooted apache after making changes to php.ini :)

YSbakker
  • 697
  • 2
  • 8
  • 27
  • have you tried if the function session_start() exists by using function_exists()? – Bernhard Apr 10 '18 at 08:39
  • Check your `session_save_path()`, for me it was `/var/lib/php5/`, needs to have `777` chmod permissions. – Sinto Apr 10 '18 at 08:40
  • Check http://php.net/manual/en/function.session-start.php esp. regarding return values and differences between php versions – Eriks Klotins Apr 10 '18 at 08:42
  • try to put `error_reporting(E_ALL);` to see the errors – Khrisna Gunanasurya Apr 10 '18 at 08:42
  • unable to reproduce – mega6382 Apr 10 '18 at 08:42
  • seems like you upgraded your php version as you wrote you updated your server, the issue can be somewhere else not in the sessions, turn on reporting to see what goes wrong. – Danyal Sandeelo Apr 10 '18 at 08:44
  • @Bernhard `function_exists()` returns 1 for session_start. @Sinto `/var/lib/php5` doesn't exist. My sessions seem to be stored in `/var/lib/php/sessions`, I gave `777` permissions to both `/php` and `/sessions` @KhrisnaGunanasurya No errors :/ – YSbakker Apr 10 '18 at 09:20
  • 1
    any errors in /var/log/apache2/error.log (or the log file for the site, if it is different)? – Karsten Koop Apr 10 '18 at 09:27
  • 1
    @KarstenKoop Wow, that was a great suggestion actually. My log files are located in `/var/lib/httpd/domains/....`. The last few lines are `Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/admin/tmp) in Unknown on line 0`. I personally think this is rather strange, anyone know if this is some sort of hardcoded default directory for sessions? There were other sessions in `/var/lib/php/sessions`, that's what's really confusing. I'll set the session.save_path accordingly and change the permissions. – YSbakker Apr 10 '18 at 09:55

2 Answers2

0

Okay, I've figured it out! Thanks everyone for your suggestions, turns out I need to learn to read error logs ...

I use vestacp and apparently it defaults the PHP sessions to /home/admin/tmp, and I'm not sure how but you're not able to change that (which I did). There was another problem though, the ownership and permissions weren't correct for that folder. Turns out all I needed was a chmod -R 700 /home/admin/tmp and chown -R root /home/admin/tmp - which I already tried on the other folder but it didn't allow that.

Btw, here's the error line that triggered the 'oh what the hell I can't change the session directory':

Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/admin/tmp) in Unknown on line 0
YSbakker
  • 697
  • 2
  • 8
  • 27
-1

session1.php

session_start();

$_SESSION['user'] = 'Test';

header('location: session2.php');
exit;
?>

session2.php

Define variable with session $_SESSION['user']

session_start();

print_r($_SESSION['user']);

?>
  • `$_SESSION` is the array that contains all session variables, `$_SESSION[“user”]` is merely a part of that array. It’d give the same result, being ‘null’ – YSbakker Apr 10 '18 at 09:08