php setcookie 域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2285010/
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 05:52:52  来源:igfitidea点击:

php setcookie domain

phpcookiessetcookie

提问by Nathan H

Some application, not written by me, and not in PHP, creates a cookie for the domain "www.domain.com".

一些不是我写的,也不是用 PHP 编写的应用程序为域“www.domain.com”创建了一个 cookie。

I am trying to replace that cookie. So in php I did:

我正在尝试替换那个 cookie。所以在php中我做了:

setcookie('mycookie','mydata',time() + 2*7*24*60*60,'/','www.domain.com', false);

However the resulting cookie is created for domain: ".www.domain.com", note the dot "." ahead of the domain.

但是,生成的 cookie 是为域创建的:“.www.domain.com”,注意点“。” 在域之前。

So it doesn't replace it, it creates another cookie. What can I do?

所以它不会替换它,它会创建另一个 cookie。我能做什么?

回答by Select0r

The issue is also adressed here: https://www.php.net/manual/en/function.setcookie.php

这个问题也在这里解决:https://www.php.net/manual/en/function.setcookie.php

See comment by jah:

请参阅jah 的评论:

If you want to restrict the cookie to a single host, supply the domain parameter as an empty string

如果要将 cookie 限制为单个主机,请将域参数作为空字符串提供

You could also try .domain.comas the domain. The trailing dot will allow a cookie for all subdomains for domain.comand could overwrite the www.-cookie, but I'll go with the above solution first.

您也可以尝试.domain.com作为域。尾随点将允许所有子域的 cookiedomain.com并可以覆盖www.-cookie,但我将首先使用上述解决方案。

回答by Gumbo

If you specify a domain, you should follow RFC 2109 and prefix the domain with a dot; otherwise the client will do that. But if you don't specify a domain at all, the client will take the domain of the request.

如果指定域,则应遵循 RFC 2109 并在域前添加点号;否则客户会这样做。但是,如果您根本不指定域,则客户端将获取请求的域。

回答by adrianTNT

Isn't this like a bug ? What if I want my cookies to just be at www.example.comand not at something.www.example.com? e.g for performance. I should be able to specify cookie domain and NOT wildcard of all [sub][sub]subdomains. Not to mention the amount of bugs it causes, for example setting cookie by php and trying to remove it by JavaScript (which doesn't add the stupid dot).

这不是一个bug吗?如果我希望我的 cookie 只是在www.example.com而不是在something.www.example.com怎么办?例如性能。我应该能够指定 cookie 域而不是所有 [sub][sub] 子域的通配符。更不用说它导致的错误数量,例如通过 php 设置 cookie 并尝试通过 JavaScript 删除它(这不会添加愚蠢的点)。

回答by Donny Kurnia

Try to create several other cookie with same name, but a different domain. Example:

尝试创建多个具有相同名称但域不同的其他 cookie。例子:

setcookie('mycookie','mydata1',time() + 2*7*24*60*60,'/','www.domain.com', false);
setcookie('mycookie','mydata2',time() + 2*7*24*60*60,'/','www.domain.com', false);
setcookie('mycookie_top','mydata1',time() + 2*7*24*60*60,'/','domain.com', false);
setcookie('mycookie_top','mydata2',time() + 2*7*24*60*60,'/','domain.com', false);

Then inspect the cookie created by these command in the Firebug. If you kept getting a double cookie, then this might be a bug in the PHP. Also, try to set the cookie in the javascript code, see if you still got the same problems.

然后在 Firebug 中检查这些命令创建的 cookie。如果您一直收到双重 cookie,那么这可能是 PHP 中的一个错误。另外,尝试在javascript代码中设置cookie,看看您是否仍然遇到相同的问题。