php 将 cookie 路径设置为“/”不会使整个站点都可以访问 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1829147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:58:35  来源:igfitidea点击:

Setting cookie path to "/" does not make cookie accessible to entire site

phpcookies

提问by troelskn

Why when set php cookie path to "/" doesn't work for every subdirs in the domain, but just for the current directory.

为什么将 php cookie 路径设置为“/”时不适用于域中的每个子目录,而仅适用于当前目录。

cookie is set like:

cookie 设置如下:

setcookie("name", "val", expire_time, "/");

It just doesn't want to work.

它只是不想工作。

回答by jlb

try including the domain parameter:

尝试包括域参数:

setcookie("name", "val", expire_time, "/", ".domain.com");
// don't forget the prefixing period: .domain.com

that will enable all sudomains of "domain.com"

这将启用“domain.com”的所有 sudomains

回答by troelskn

Are you testing on localhost? In that case, you need to pass null as the value for $domain.

您在本地主机上测试吗?在这种情况下,您需要将 null 作为 的值传递$domain

回答by Atli

Setting the cookie path to /should make it available to the entire domain. If you set your cookie like that, and it isn't being sent, there is something else wrong.

将 cookie 路径设置为/应使其可用于整个域。如果你这样设置你的 cookie,并且它没有被发送,那么还有其他问题。

Try using the Web Developer addon in Firefox. It shows you details on the available cookies. Maybe that can help you diagnose the problem.

尝试在 Firefox 中使用 Web Developer 插件。它向您显示有关可用 cookie 的详细信息。也许这可以帮助您诊断问题。

回答by Phil LaNasa

Late to the party, I know. But I just discovered that my issue was pretty stupid, but I'll post it for completion:

我知道,聚会迟到了。但我刚刚发现我的问题很愚蠢,但我会发布它以供完成:

I was neglecting to add time() to the expires time on the cookie, so it was expiring immediately.

我忽略了将 time() 添加到 cookie 的过期时间,因此它立即过期。

The expires time should be time() + seconds

过期时间应该是 time() + seconds