Edit 29/1/2019 – this question was eligible for a bounty but none of the provided answers directly address the issue. If you have an answer that works based on what’s been asked please respond
I have 2 domains configured on the same physical server:
app.example.com
help.app.example.com
Users login to https://app.example.com/
which consists of PHP 5.5 application which stores some data in the $_SESSION
array after a successful login.
I want to configure https://help.app.example.com/
so that I can read the session data present on https://app.example.com/
. The application on the help
subdomain is a content management system built in PHP 7.
In Plesk I have added the following to ‘Additional configuration directives’ for php.ini
under both app.example.com
and help.app.example.com
:
session.cookie_domain = ".example.com"
If I upload a phpinfo()
script to help.app.example.com
it is showing the following for session.cookie_domain
:
- Local value: no value
- Master value:
.example.com
If I then run the following in a script on help.app.example.com
:
<?php
session_start();
var_dump($_SESSION);
die;
?>
It is outputting an empty array:
array(0) { }
However, if I run the equivalent on app.example.com
it is outputting an array of session data which shows details of the logged-in user (as expected):
array(15) {
["o_id"]=> (1) "1"
["u_id"]=> string(4) "1745"
...
}
I’m expecting to see the same output on both sub-domains. Why is this not working?
I have read Allow php sessions to carry over to subdomains but none of that resolves the problem.
2
Answers
I just try to replicate whit:
and works pretty fine. I got values in _Session array in both cases, even I just save the values only in the subdomain.
In my set, the ‘Additional configuration directives’ for php.ini is the same for the domain and the subdomain.
by default sessions are saved to local files on the server, location of which is specified in php.ini’s
session.save_path
, for examplesession.save_path = /var/lib/php/sessions
, if app.example.com and help.app.example.com are running on 2 different servers with their own filesystem, or even if it’s running on the same filesystem but have differentsession.save_path
directives in php.ini, they won’t share the same $_SESSION.if you want 2 different servers to share the same $_SESSION, possible solutions include creating a shared session store database with
session_set_save_handler()
(like MongoDB or MySQL comes to mind), or creating a networked filesystem and setsession.save_path = /path/to/networked/filesystem/mountpoint
in php.ini, but both of these methods may incur a significant performance penalty..… since the cookie is shared across both domains,
session_id()
will return the same value on both sides, that could be used as an id for a session database, take a look at http://php.net/manual/en/class.sessionhandlerinterface.php(i’d write a sample class if i had more time but i’m out of time)switch to a sql-db-backed session store (like MariaDB, MySQL, or PostgreSQL), for example:
schema:
SessionHandlerInterface implementation:
usage: