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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...