title: PHP cookie and session values not recognized on first attempt
body: <p>OK, so here's my second attempt at this, with a new and much more isolated and concise way of demonstrating the problem. The problem pertains to a login system where session vars do not seem to save on the first attempt. But here is the stripped down code that demonstrates the errant behavior:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  echo "POST superglobal: &lt;pre&gt;" . print_r($_POST, true) . "&lt;/pre&gt;&lt;hr/&gt;";
  echo "SESSION superglobal: &lt;pre&gt;" . print_r($_SESSION, true) . "&lt;/pre&gt;&lt;hr/&gt;";
  echo "COOKIE superglobal: &lt;pre&gt;" . print_r($_COOKIE, true) . "&lt;/pre&gt;&lt;hr/&gt;";
  
  $_SESSION['session_var'] = "SESSION VAR AVAILABLE";
  setcookie("cookie_var", "COOKIE VAR AVAILABLE");
  
  ?&gt;  
  &lt;html&gt;
      &lt;body&gt;
          &lt;form action="" name="frmPost" method="POST"&gt;
              &lt;input type="submit" name="cmdSubmitPost" value="Submit" /&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>On my localhost on my local computer, here's what this code produces:</p>
  
  <p>After initial page load:</p>
  
  <pre><code>POST superglobal:
  Array
  (
  )
  
  SESSION superglobal:
  Array
  (
  )
  
  COOKIE superglobal:
  Array
  (
  )
  
  [Submit]
  </code></pre>
  
  <p>After clicking <code>Submit</code> one time:</p>
  
  <pre><code>POST superglobal:
  Array
  (
      [cmdSubmitPost] =&gt; Submit
  )
  
  SESSION superglobal:
  Array
  (
      [session_var] =&gt; SESSION VAR AVAILABLE
  )
  
  COOKIE superglobal:
  Array
  (
      [cookie_var] =&gt; COOKIE VAR AVAILABLE
      [PHPSESSID] =&gt; hpkpft4hh1tqlm6e3r496bt5j2
  )
  
  [Submit]
  </code></pre>
  
  <p>This is exactly what I expect and need. I also signed up temporarily on two separate free php hosting sites to test this, and they both yielded the same results as above.</p>
  
  <p>However, at Arvixe, my main hosting environment, the results are different. There it goes like this:</p>
  
  <p>After initial page load:</p>
  
  <pre><code>POST superglobal:
  Array
  (
  )
  
  SESSION superglobal:
  Array
  (
  )
  
  COOKIE superglobal:
  Array
  (
  )
  
  [Submit]
  </code></pre>
  
  <p>After clicking <code>Submit</code> one time:</p>
  
  <pre><code>POST superglobal:
  Array
  (
      [cmdSubmitPost] =&gt; Submit
  )
  
  SESSION superglobal:
  Array
  (
  )
  
  COOKIE superglobal:
  Array
  (
  )
  
  [Submit]
  </code></pre>
  
  <p>After clicking <code>Submit</code> a SECOND time:</p>
  
  <pre><code>POST superglobal:
  Array
  (
      [cmdSubmitPost] =&gt; Submit
  )
  
  SESSION superglobal:
  Array
  (
      [session_var] =&gt; SESSION VAR AVAILABLE
  )
  
  COOKIE superglobal:
  Array
  (
      [cookie_var] =&gt; COOKIE VAR AVAILABLE
      [PHPSESSID] =&gt; 2k6ldfl2icdtj1k7sq5dc9qmj3
      [VC-NoCache] =&gt; 1
      [_asomcnc] =&gt; 1
  )
  
  [Submit]
  </code></pre>
  
  <p>I hope that the results created in the localhost and other temporary free php hosting environments rules out any fundamental flaw in the fairly straightforward code.</p>
  
  <p>OK, so NOW the question: In the Arvixe hosting environment why does the page not recognize the cookie and session values after the first submit? I'm pretty sure that the REAL question is why is the cookie value not recognized after the first submit? (that's the real question because that answers why the session value isn't being recognized... it's because the PHPSESSID cookie isn't yet recognized).</p>
  
  <p>The php.ini values are essentially the same, but if there are particular values that may help to publish here, let me know which ones.</p>
  
  <p>ALSO, I assume that the answer to this question would effectively solve these other three stackoverflow questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/8846594/php-cookie-will-not-set-until-the-page-reloads-twice-whats-going-on">PHP cookie will not set until the page reloads TWICE. What&#39;s going on?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/21469506/php-session-variable-not-stored-at-first-attempt-working-fine-from-second-attem">php session variable not stored at first attempt, working fine from second attempt</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11959919/session-variables-not-set-the-first-time">session variables not set the first time</a></p>
  
  <p>(none of the solutions there worked for me)</p>