这是一个小测试.我设置一个cookie,然后尝试访问它:
setcookie("t",0,time()+900);
echo ($_COOKIE['t']+10);
setcookie("t",0,time()-3600);
?>
运行代码时,出现如下错误消息:
Notice: Undefined index: t in /var/www/x/testcookie.php on line 5
10
为什么我无法访问Cookie?
解决方法:
那样行不通. setcookie只是说“通过下一个http连接告诉客户端(浏览器)设置此cookie.浏览器会在下一个http连接中将其发送回(如果尚未过期).只有这样它才包含在$_COOKIE数组中.因此,您可以检查一下在下一页重新加载后在PHP中进行设置.
除了在您的代码中,不会设置第二个cookie,因为您在setcookie函数(任何标头函数)之前向浏览器输出了某些禁止的东西.
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
Once the cookies have been set, they can be accessed on the next page
load with the $_COOKIE
标签:cookies,localhost,session-cookies,php
来源: https://codeday.me/bug/20191028/1952525.html