Hi all,
I'm having a very annoying problem with cookies. I tried to run this example code I found in a PHP cookies tutorial:
<?php
if (!$_COOKIE["cookie"])
{
setcookie("cookie","I am a cookie.",time()+600);
echo "No cookie was found. Created a cookie of name 'cookie'.";
}
else //if the cookie exists
{
setcookie("cookie",NULL,time()-1);
echo "You've already been in this page. Deleted the cookie (its value was: <B>".$_COOKIE["cookie"]."</B>).";
}
?>
This code is supposed to check if the cookie "cookie" exists. If it does, it creates it and says that no cookie was found; if it does, it deletes it and says you've already been in this page. The user is supposed to see a different message each time he loads the page.
When I check if the cookie exists with
if (!$_COOKIE["cookie"])
, I get a notice: Notice: Undefined index: cookie (with the line number etc.). The problem is that this notice makes PHP send the headers and then I get the error message: "Cannot modify header information - headers already sent by...". Consequently, I get the same notification each time and the cookie is never created.
Can anyone help me with that?
Thanks a lot!! 🙂