title: share sessions between subdomains PHP
body: <p>I have the following PHP-code at the beginning of every file:</p>
  
  <pre><code>session_set_cookie_params(0, '/', 'localhost');
  session_start();
  </code></pre>
  
  <p>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.</p>
  
  <p>How can I share sessions between subdomains on localhost (and online)?</p>
  
  <p>Subdomains:</p>
  
  <ul>
  <li>account.localhost</li>
  <li>helpdesk.localhost</li>
  </ul>
  
  <p>account.localhost points to the login file, but I want users to be logged in on the helpdesk.localhost-subdomain as well.</p>
  
  <p>EDIT: it's about <strong>subdomains</strong>, not different domains.</p>
  
  <p>EDIT 2:</p>
  
  <p>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).</p>
  
  <p><strong>php.ini</strong></p>
  
  <pre><code>session.cookie_domain = ".example.com"
  </code></pre>
  
  <p><strong>.htaccess</strong></p>
  
  <pre><code>php_value session.cookie_domain .example.com
  </code></pre>
  
  <p><strong>before session_start()</strong></p>
  
  <pre><code>ini_set('session.cookie_domain', '.example.com');
  </code></pre>
  
  <p>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.</p>
  
  <p>EDIT 3:</p>
  
  <p>I'm working on XAMPP on Windows 10. I changed the /etc/hosts to the following lines:</p>
  
  <pre><code>127.0.0.1 account.domain.local
  127.0.0.1 helpdesk.domain.local
  </code></pre>
  
  <p>It's working now!</p>