微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

setcookie php非常慢或发生了什么?

这是一个小测试.我设置一个cookie,然后尝试访问它:

<?PHP 
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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐