繁体   English   中英

在 PHP header() 与 setcookie() 中设置 cookie 到期时间的问题

[英]issue with setting expiry time of cookie in PHP header() vs setcookie()

我想使用header() function 设置一个有效期为 30 天的 cookie,如下所示:

$exptime=time()+30*24*60*60;
header("Set-Cookie: __try1=usingheader; expires=$exptime; path=/");

但这不起作用,只是设置了一个session cookie。 同样的事情适用于setcookie() function

setcookie("__try2", "usingsetcookie", $exptime, '/');

响应头如下:

Set-Cookie: __try1=usingheader; expires=1613126399; path=/
Set-Cookie: __try2=usingsetcookie; expires=Fri, 12-Feb-2021 10:39:59 GMT; Max-Age=2592000; path=/

将不胜感激任何帮助。

expires 标志的正确时间格式类似于Tue, 19 Jan 2021 15:40:59 GMT 我们可以使用gmdate()生成这种格式。 以下是将到期时间设置为从当前时间起 6 个月的示例。

$expirytime = gmdate("D, d-M-Y H:i:s T", strtotime( '+6 months' ));

或者因为 PHP 5.1.1 我们可以使用下面的。 请参阅参考资料

echo gmdate(DATE_COOKIE , strtotime( '+6 months' ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM