Reputation: 617
I'm sending a variable via link:
<a href="foo.php?var=101"> php </a>
<a href="foo.php?var=102"> html </a>
<a href="foo.php?var=102"> css </a>
and on foo.php
:
<!-- this part is under data-role="page" !-->
if(isset($_GET['var'])){
switch($_GET['var']){
case "101":
setcookie("bar", "1");
break;
case "102":
setcookie("bar", "2");
break;
case "103":
setcookie("bar", "3");
break;
default:
setcookie("bar", "");
}
}
<!-- this part is under contents !-->
echo $_COOKIE['bar'];
now the problem is: lets say Im on index.php
and I click on link php
, i get nothing on foo.php
, if I go back and click html
I get php
which is unusual, same if i go back and click on css
after that, I get html
all my JS files are under index.php
's head, why am I getting the old value?
Upvotes: 0
Views: 23
Reputation: 1102
The problem comes from the fact that setcookie() doesn't set the cookies immediately, it sends the headers so the browser sets the cookies. This means that, for the current page load, setcookie() will no generate any $_COOKIE.
When the browser later on requests a page, it sends the cookies in the headers so the PHP can retrieve them in the form of $_COOKIE.
Upvotes: 2