php写入cookie很慢,setcookie php非常慢或发生了什么?

这是一个小测试.我设置一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值