title: PHP creating new session with each reload
body: <p>For my website, session management mostly works ok. Sessions are created, saved and used later without problems.</p>
  
  <p>But when the code is using session_start(), it always creates new, totally empty session. Code in question below. </p>
  
  <pre><code>header('Content-Type: text/html; charset=UTF-8');
  
  $main_domain = $_SERVER["HTTP_HOST"];
  $expld = explode('.', $main_domain);
  
  if(count($expld) &gt; 2) {
     $tld = array_pop($expld);
     $domain = array_pop($expld);
     $main_domain = $domain . "." . $tld;
  }
  
  session_set_cookie_params (0, '/', $main_domain);
  session_name('sid');
  session_start();
  echo session_id();
  exit;
  </code></pre>
  
  <p>When this script is executed, in every reload new session is created. </p>
  
  <pre><code>smar@ran ~&gt; ls /tmp/sess_* | wc -l
  10
  smar@ran ~&gt; ls /tmp/sess_* | wc -l
  11
  ..
  smar@ran ~&gt; ls /tmp/sess_* | wc -l
  17
  </code></pre>
  
  <p>But only the one of those sessions has any data inside it, and is used by application. </p>
  
  <p>Output in browser is always same: <code>87412d5882jr85gh5mkasmngg7</code>, which is id in browser’s cookie and session id in /tmp that has data populated to it. </p>
  
  <p>What could be cause of this behaviour? Those empty files aren’t exactly huge problem, but they do make /tmp (or session dir) quite populated for no reason. </p>
  
  <p>EDIT 1:</p>
  
  <p>Looks like this is server related problem, since it works for some people. My configuration is Gentoo Linux (32 bit) with Apache and PHP 5.3.6. </p>
  
  <p>If I force it to create new session (like removing my own cookie), it creates <em>two</em> session files instead of one. If it reuses old one, it creates “only” one. </p>
  
  <p>EDIT 2:</p>
  
  <p>Session configuration, as requested (all config rows with <code>session.</code>):</p>
  
  <pre><code>session.save_handler = files
  session.save_path = "/tmp"
  session.use_cookies = 1
  session.use_only_cookies = 1
  session.name = PHPSESSID
  session.auto_start = 0
  session.cookie_lifetime = 0
  session.cookie_path = /
  session.cookie_domain =
  session.cookie_httponly =
  session.serialize_handler = php
  session.gc_probability = 1
  session.gc_divisor = 1000
  session.gc_maxlifetime = 1440
  session.bug_compat_42 = On
  session.bug_compat_warn = On
  session.referer_check =
  session.entropy_length = 0
  session.entropy_file =
  session.cache_limiter = nocache
  session.cache_expire = 180
  session.use_trans_sid = 0
  session.hash_function = 0
  session.hash_bits_per_character = 5
  </code></pre>
  
  <p>EDIT 3:</p>
  
  <p>Even more strangely, I tried to use sessions from CLI. There, where no session cookies are set, it always created one new session. When setting fixed session value with <code>session_id()</code> stopped new session creation altogether and used old session instead.</p>
  
  <p>This behaviour is identical with Apache, so I’m starting to suspect this is bug in PHP. No new sessions created if name specially set with <code>session_id()</code>, and session correctly used. </p>
  
  <p>Even more absurdly, when I took phpsessid from <code>$_COOKIE["PHPSESSID"]</code> and set that to session_id(), it started to create new (useless empty ones) sessions again. </p>
  
  <p>EDIT 4:</p>
  
  <p>Since I didn’t write it enough clearly: simply having</p>
  
  <pre><code>session_start()
  </code></pre>
  
  <p>as single argument causes this problem to happen, it is not specific to my code. </p>