gpt4 book ai didi

php - setcookie php 很慢或者发生了什么?

转载 作者:可可西里 更新时间:2023-11-01 01:00:12 26 4
gpt4 key购买 nike

这是一个小测试。我设置了一个 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 函数(任何 header 函数)之前向浏览器输出了一些被禁止的内容。

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

关于php - setcookie php 很慢或者发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29701469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com