-1

This post makes it seem like I should immediately be able to access a cookie after creating it if I specify its name:

$username = "thomas";

setcookie("logs_sign_in", $username, time() + 60 * 60 * 24 * 30, ".");

echo var_dump($_COOKIE['logs_sign_in']);

However I still get NULL. I only the name after I refresh the page. Why is this?

1
  • 1
    Since it is the first related post, it probably came up as a suggestion when you were asking yours. They are there for a reason. Dec 9, 2013 at 20:04

1 Answer 1

3

This is simply how cookies work:

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. (setcookie manual page)

This actually makes a great deal of sense. Look at the manual page for $_COOKIE:

An associative array of variables passed to the current script via HTTP Cookies.

The value wasn't passed to the current script via HTTP Cookies, so it isn't in the $_COOKIE array.

Not the answer you're looking for? Browse other questions tagged or ask your own question.