If I use the following basic code
if (!defined('NAME_COOKIE') )
define('NAME_COOKIE', "storedusername");
$cookie_domain = ".".$_SERVER['HTTP_HOST'];
setcookie(NAME_COOKIE, $username,time() + (86400),"/", $cookie_domain);
print $_COOKIE[NAME_COOKIE];
The script dies during the print with undefined index error. What am I doing wrong?
Your lines:
setcookie(NAME_COOKIE, $username,time() + (86400),"/", $cookie_domain);
print $_COOKIE[NAME_COOKIE];
Whats happening here is that your setting the cookie, which means that a string is *added to the headers, ready to send with your content.
think of this like a queue, and the queue goes to the browser only when you send your content.
as your cookie is still in the queue, its not actually been set until the page gets sent and you recall the page, then upon the recall the browser will send the cookie information back to the browser which in turn compiles the $_COOKIE
array.
Try think of it like this:
$_COOKIE
$_COOKIE
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With