0

I am using this code to set a cookie and then see if they exist

setcookie("token", "value", time()+60*60*24*100, "/");
setcookie("secret", "value", time()+60*60*24*100, "/");
setcookie("key", "value", time()+60*60*24*100, "/");

if (!isset($_COOKIE['token']) || !isset($_COOKIE['secret']) || !isset($_COOKIE['key'])) {

// do something because one of the cookies were not set

}

Even though all three of the cookies were set in my browser, it still runs the if() statement. Via the process of elimination I have discovered the middle cookie !isset($_COOKIE['secret']) seems to cause the if() statement to run even though the cookie secret was set in my browser. The script says it has not been set when I look at my browser and it has been set. Can you think of any reason why php is saying it wasn't set?

Frank
  • 1,844
  • 8
  • 29
  • 44
  • your code works for me. the block is executed on the first visit as $_COOKIE is not filled with your values from setCookie, yet. on further calls, the block is not executed anymore. do you have, by any chance, installed a cookie blocker? – Basti Feb 23 '12 at 14:53
  • 1
    What tool are you using to tell if that cookie exists? Firebug? Seems pretty strange... – Jovan Perovic Feb 23 '12 at 14:54

3 Answers3

7

setcookie only defines a cookie to be sent along with the rest of the HTTP headers, and they can be accessed on the next page load with the $_COOKIE. With your code, the HTTP headers are not be sent.

You just need setcookie when a cookie is not set. Like:

if (!isset($_COOKIE['token'])) {
    setcookie("token", "value", time()+60*60*24*100, "/");
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • don't use $_COOKIE to store your information. have separated variables $token, $secret and $key. and load these with the values from $_COOKIE. if $_COOKIE is empty, initialize them manually and call setCookie. – Basti Feb 23 '12 at 14:58
  • or if the user does not have the cookies set, do `setCookie` and relocate the user to the same site again with `header()`. – Basti Feb 23 '12 at 14:59
  • You could also force the cookie into the array after you set it if you want to immediately access it... `setcookie("token", "value");` then `$_COOKIE['token'] = "value";` – fire Feb 23 '12 at 15:26
0

as my testing,we can't use cookies in same time.if you set cookies. you need to reload page to grab those. put like this

if (!isset($_COOKIE['token'])) {
    setcookie("token", "value", time()+60*60*24*100, "/"); //this set cookies for first time
}
0

use

if(true === array_key_exists('secret', $_COOKIE) && strlen($_COOKIE['secret']) > 0) {
}
silly
  • 7,789
  • 2
  • 24
  • 37
  • 2
    That doesn't answer my question, why wont my code work? when it works for the other two cookies. – Frank Feb 23 '12 at 14:50
  • 1
    `array_key_exists($_COOKIE, 'secret') === isset($_COOKIE['secret'])` and OP didn' want to check if it was empty. the issue still remains – Basti Feb 23 '12 at 14:52
  • 1
    @Basti sorry is not the same, if array_key_exists checking if the key exists, isset check if the value exist, whenn the value is null the isset will return false. – silly Feb 23 '12 at 14:58
  • you'r right. `array_key_exists` also assumes `$_COOKIE` to be an array. if it's not, `array_key_exists` will fail with a warning. but we can safely assume that $_COOKIE will be of type array for every script. we can also assume that the values in $_COOKIE are strings, as they get passed in the HTTP-Request. so when using $_COOKIE those two calls are in fact the same, unless you tinker with $_COOKIE beforehand. – Basti Feb 23 '12 at 15:02
  • 1
    Hate chiming onto something so old, but for posterity, `array_key_exists` is backwards in the example. It should be `array_key_exists('secret', $_COOKIE)`. – John Green Aug 22 '18 at 20:53