0

For some reason this code does not set a cookie on my site, but the code afterwards is being executed :/

<?php
if (!isset($_COOKIE["drop"])){
setcookie("drop",true);
//do code for one time until cookies are deleted
}
?>

Edit: Just tested it on my laptop running lamp, it works perfectly. Can there be a php setting on my webhosting that's wrong?

3 Answers3

0

Try the same code as .... First time cookies gives a problem.. Try reloading the page again.

<?php
if (!isset($_COOKIE["drop"])){
setcookie("drop",true);
//do code for one time until cookies are deleted
}
else
{
echo "Cookie is set...";
}

?>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Try using "array_key_exists" as opposed to "isset". This can create errors depending on the PHP version.

Also, are you sure it's not setting? How are you checking it?

Tom
  • 207
  • 2
  • 18
0

If you not provide the third argument of the setcookie() function. Your cookie gets deleted after the user closes the browser. So it's like a session variable.

Also the browser must accept cookies that you can use them.

And the cookie is available in the $_COOKIE variable in your next request after you set the cookie with setcookie(). As mentioned here.

Community
  • 1
  • 1
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
  • It works as long as you not close the browser. But if you close your browser (or the tab of your browser with the site in it). The session gets destroyed and also the cookie. That's what the PHP doc tells about setcookie(). – TiMESPLiNTER Oct 29 '13 at 13:19