PHP sessions: $_SESSION is empty running locally on Mac

Viewed 442

I know there are similar other questions here, but none of them solved my problem.

The problem: I'm setting data into $_SESSION, but in upcoming calls it is appearing as empty.

What I did to try solving it?

  1. Make sure to run an up-to-date version of PHP
  2. Make sure to know which php.ini is used
  3. Log status of $_SESSION before and after setting it
  4. Set various variables as suggested in other questions and forums

Here below I provide all the info I could supply, and hoping somebody will be able to suggest a solution!


I have installed PHP on my Mac using brew install php

I am running it locally for software development using this command:

php -S localhost:8099 -d display_errors=on -t .
[Tue Nov 17 23:12:55 2020] PHP 7.4.11 Development Server (http://localhost:8099) started

I'm calling session_start(); at the beginning of my script. I am setting values into $_SESSION, but it is always empty when I am testing it in the following calls:

$ses = json_encode($_SESSION);
site_log("before: {$ses}");

session_regenerate_id();
$_SESSION["upath"] = 'XXXXXX';

$ses = json_encode($_SESSION);
site_log("after: {$ses}");

The result is always the same (while I'm expecting to see it on first call but not for the following calls):

20201118_040124_484 before: []
20201118_040124_485 after: {"upath":"XXXXXX"}

I was trying to follow many ideas from web forums:

Verify which is the INI file:

php -i | grep 'Configuration File'
>Configuration File (php.ini) Path => /usr/local/etc/php/7.4
>Loaded Configuration File => /usr/local/etc/php/7.4/php.ini

I was setting various INI file variables:

session.save_path = "/Users/myuser/Prog/MyApp/sessions"
+
chmod 777 /Users/myuser/Prog/MyApp/sessions
+
session.use_cookies = 1
+
session.cookie_secure = 0

But the result is still the same. Any idea please?

The following link points to the result of calling phpinfo(): link

1 Answers

So finally I have found a solution.

It is based on information provided here: Session cookie not being set

As @deceze was commenting correctly to my question, the first steps to debug such problem are these:

  1. Check whether session files are created in the save_path folder (in my case: yes)
  2. Use the dev-tools of the browser to see if cookies are set for the site (in my case: no)

So the actual problem is: why cookies are not set?

The solution:

  1. Add an entry to alias 127.0.0.1 as a 'dot com' name. This is what I did:
# for Mac! see linked answer for other platforms

sudo vi /private/etc/hosts 

# add this line:
127.0.0.1 mylocal.com
  1. Activate the local php server for this domain (from the site root folder):
php -S mylocal.com:8099 -d display_errors=on -t .
  1. Load the site from the php server. I'm using Brackets, and I had to open the menu: File -> Project Settings and set the live preview base URL to be:
http://mylocal.com:8099/
  1. Some variables in the INI file might need to be set too, as already detailed in the question above

Hoping this info will help to others!

Related