Reputation: 33
I run this simple example, but i see "Undefined index: color" instead of expected "red". But after I refresh the page, i see "red". So what is my mistake? Thanks.
<?php
setcookie("color","red");
echo $_COOKIE["color"];
?>
Upvotes: 0
Views: 132
Reputation: 1375
The reason for this is that your setcookie function does not put the value into the $_COOKIE.
During the refresh (when a new HTTP request is made) the $_COOKIE gets loaded with all the updated values and therefore you see it then.
Upvotes: 2