-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?

Community
  • 1
  • 1
1252748
  • 14,597
  • 32
  • 109
  • 229
  • 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. – Tim Seguine Dec 09 '13 at 20:04

1 Answers1

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.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318