0

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!

Sam Leurs
  • 480
  • 4
  • 20
  • 1
    Possible duplicate of [Preserving session variables across different domains](https://stackoverflow.com/questions/14611545/preserving-session-variables-across-different-domains) – JustBaron Jul 16 '18 at 13:24
  • that question is about cross-domain sessions, not subdomains. – Sam Leurs Jul 16 '18 at 13:28
  • [Try this](https://stackoverflow.com/questions/9153716/sharing-session-variables-between-multiple-subdomains) or [This one](https://stackoverflow.com/questions/644920/allow-php-sessions-to-carry-over-to-subdomains) – JustBaron Jul 16 '18 at 13:29
  • thanks, I'll try it. – Sam Leurs Jul 16 '18 at 13:30

0 Answers0