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:
<?php
session_start();
echo "POST superglobal: <pre>" . print_r($_POST, true) . "</pre><hr/>";
echo "SESSION superglobal: <pre>" . print_r($_SESSION, true) . "</pre><hr/>";
echo "COOKIE superglobal: <pre>" . print_r($_COOKIE, true) . "</pre><hr/>";
$_SESSION['session_var'] = "SESSION VAR AVAILABLE";
setcookie("cookie_var", "COOKIE VAR AVAILABLE");
?>
<html>
<body>
<form action="" name="frmPost" method="POST">
<input type="submit" name="cmdSubmitPost" value="Submit" />
</form>
</body>
</html>
On my localhost on my local computer, here's what this code produces:
After initial page load:
POST superglobal:
Array
(
)
SESSION superglobal:
Array
(
)
COOKIE superglobal:
Array
(
)
[Submit]
After clicking Submit
one time:
POST superglobal:
Array
(
[cmdSubmitPost] => Submit
)
SESSION superglobal:
Array
(
[session_var] => SESSION VAR AVAILABLE
)
COOKIE superglobal:
Array
(
[cookie_var] => COOKIE VAR AVAILABLE
[PHPSESSID] => hpkpft4hh1tqlm6e3r496bt5j2
)
[Submit]
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.
However, at Arvixe, my main hosting environment, the results are different. There it goes like this:
After initial page load:
POST superglobal:
Array
(
)
SESSION superglobal:
Array
(
)
COOKIE superglobal:
Array
(
)
[Submit]
After clicking Submit
one time:
POST superglobal:
Array
(
[cmdSubmitPost] => Submit
)
SESSION superglobal:
Array
(
)
COOKIE superglobal:
Array
(
)
[Submit]
After clicking Submit
a SECOND time:
POST superglobal:
Array
(
[cmdSubmitPost] => Submit
)
SESSION superglobal:
Array
(
[session_var] => SESSION VAR AVAILABLE
)
COOKIE superglobal:
Array
(
[cookie_var] => COOKIE VAR AVAILABLE
[PHPSESSID] => 2k6ldfl2icdtj1k7sq5dc9qmj3
[VC-NoCache] => 1
[_asomcnc] => 1
)
[Submit]
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.
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).
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.
ALSO, I assume that the answer to this question would effectively solve these other three stackoverflow questions:
PHP cookie will not set until the page reloads TWICE. What's going on?
php session variable not stored at first attempt, working fine from second attempt
session variables not set the first time
(none of the solutions there worked for me)