php $ _COOKIE isset [英] php $_COOKIE isset
问题描述
我使用此代码设置一个cookie,然后查看它们是否存在
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
}
即使所有三个cookie都在我的浏览器中设置,它仍然运行 if()
语句。通过消除过程我发现中间的cookie !isset($ _ COOKIE ['secret'])
似乎导致 if code>语句运行,即使在我的浏览器中设置了cookie
secret
。脚本说它没有设置,当我看着我的浏览器,它已经设置。
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?
推荐答案
setcookie
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.
当您没有设定Cookie时,您只需要 setcookie
。喜欢:
You just need setcookie
when a cookie is not set. Like:
if (!isset($_COOKIE['token'])) {
setcookie("token", "value", time()+60*60*24*100, "/");
}
这篇关于php $ _COOKIE isset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!