Adham
Adham

Reputation: 64854

Cookie doesn't store information in php?

Why Cookies doesn't store information in php ?

even in this simple code ..

<?
   setcookie("test","Cookies teso");
   echo "My cookie value: ".$_COOKIE["test"];
?>

Upvotes: 1

Views: 183

Answers (2)

badideas
badideas

Reputation: 3557

page needs to be refreshed.

$_COOKIE has the cookies from your browser from the start of the execution of the script. setcookie() sets the information in the browser, but that info isn't yet in the $_COOKIE array. it will be at the next page load, though

Upvotes: 5

Kasaku
Kasaku

Reputation: 2192

They will be available on the next page load. From the documentation:

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

Note that you need to set the cookies before any other script output is done:

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

Upvotes: 6

Related Questions