Siberian Breaks
Siberian Breaks

Reputation: 33

Why do i can use cookies only after I refreshed the page?

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

Answers (1)

SurrealSyntax
SurrealSyntax

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

Related Questions