I have the following PHP-code at the beginning of every file:
session_set_cookie_params(0, '/', 'localhost');
session_start();
I want to share the session cookie between subdomains on localhost (and in a later stadium, online). But this does not work. If I change "localhost" to ".localhost", it doesn't work either.
How can I share sessions between subdomains on localhost (and online)?
Subdomains:
- account.localhost
- helpdesk.localhost
account.localhost points to the login file, but I want users to be logged in on the helpdesk.localhost-subdomain as well.
EDIT: it's about subdomains, not different domains.
EDIT 2:
I found some solutions thanks to JustBaron, but they don't work on localhost (and I want to test my script first locally before I upload it).
php.ini
session.cookie_domain = ".example.com"
.htaccess
php_value session.cookie_domain .example.com
before session_start()
ini_set('session.cookie_domain', '.example.com');
When I change '.example.com' to '.localhost' (or 'localhost'), it does not work. If I do a print_r($_SESSION), I get an empty result.
EDIT 3:
I'm working on XAMPP on Windows 10. I changed the /etc/hosts to the following lines:
127.0.0.1 account.domain.local
127.0.0.1 helpdesk.domain.local
It's working now!