PHP setcookie() 用于域但不用于子域

发布于 2024-08-04 10:26:36 字数 496 浏览 1 评论 0原文

有没有办法设置在子域上可读的cookie?换句话说,让 Cookie 在 domain.com 上可用,但www.domain.comxyz.domain.com 上可用

//this is what i'm "intending"...
setcookie($name,$value,$expires,'/','domain.com');
//however, this is how it behaves:
setcookie($name,$value,$expires,'/','.domain.com');

推理:我正在子域上设置静态 CDN,并且不希望用户会话 cookie 来回处理每个图像、css 文件、js 文件等

...我是否必须回退到使用www.domain.com 适合我的网站吗?有什么解决方法吗?

Is there any way to set a cookie that is not readable on subdomains? In other words, have the cookie available on domain.com, but not www.domain.com or xyz.domain.com.

//this is what i'm "intending"...
setcookie($name,$value,$expires,'/','domain.com');
//however, this is how it behaves:
setcookie($name,$value,$expires,'/','.domain.com');

The reasoning: I'm setting up a static CDN on a subdomain and don't want the user session cookies going back and forth for every image, css file, js file, etc.

...do I have to fall back to using www.domain.com for my site? Are there any workarounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新雨望断虹 2024-08-11 10:26:36

显然,在“domain.com”上有一个与“*.domain.com”匹配的 cookie 是预期的行为。

例如:持久客户端状态 HTTP COOKIES 状态(有些是我的强调):

域=DOMAIN_NAME

在 cookie 列表中搜索
有效的cookie,比较
cookie 的域属性是
用互联网域名制作
URL 所在的主机
取来的。 ...
“尾部匹配”的意思
该域属性匹配
完全抵住尾部
主机的限定域名。 A
“acme.com”的域属性将
将主机名“anvil.acme.com”匹配为
以及“shipping.crate.acme.com”

仅指定域内的主机
可以为域设置 cookie,并且
域名必须至少有两 (2) 个
或其中三 (3) 个句点

防止以下形式的域:“.com”,
“.edu”和“va.us”。任何域
不符合七项特殊规定之一
仅下面列出的顶级域名
需要两个时期。任何其他域
至少需要三个。七人
特殊的顶级域名是:“COM”,
“EDU”、“NET”、“ORG”、“GOV”、“MIL”和
“INT”。

因此,您必须:

  • 为您的网站使用“www.domain.com”,
  • 或者为您的静态内容使用完全不同的域名(例如“.anotherdomain.com”)代码>”)
    • 例如,这就是 stackoverflow 上所做的事情:静态内容由 sstatic.net 提供

Apparently, having a cookie on "domain.com" that will match "*.domain.com" is expected behaviour.

For instance : PERSISTENT CLIENT STATE HTTP COOKIES state (some emphasis mine) :

domain=DOMAIN_NAME

When searching the cookie list for
valid cookies, a comparison of the
domain attributes of the cookie is
made with the Internet domain name of
the host from which the URL will be
fetched. ...
"Tail matching" means
that domain attribute is matched
against the tail of the fully
qualified domain name of the host. A
domain attribute of "acme.com" would
match host names "anvil.acme.com" as
well as "shipping.crate.acme.com"
.

Only hosts within the specified domain
can set a cookie for a domain and
domains must have at least two (2)
or three (3) periods
in them to
prevent domains of the form: ".com",
".edu", and "va.us". Any domain that
fails within one of the seven special
top level domains listed below only
require two periods. Any other domain
requires at least three. The seven
special top level domains are: "COM",
"EDU", "NET", "ORG", "GOV", "MIL", and
"INT".

So, you'll either have to :

  • use "www.domain.com" for your site
  • or use a totally different domain name for your static content (like ".anotherdomain.com")
    • for instance, this is what is done on stackoverflow : static content is served from sstatic.net
孤独患者 2024-08-11 10:26:36

这就是为什么相当多的网站(包括这个网站)注册专用域名用作 CDN 的原因。

this is the reason why quite a few sites (including this one) register a dedicated domain for use as a CDN.

海风掠过北极光 2024-08-11 10:26:36

当然可以!这就是大多数网站所做的。甚至内置的 php 函数 session_start() 也能做到这一点。它的 Set-Cookie http 响应标头看起来就像这样简单:

Set-Cookie: PHPSESSID=fe26eaac143ef75ffcbc91bbe5780d0d; path=/

根据 RFC 6265,第 4.1.2.3 节,该段落中的最后一条语句:

如果服务器省略了Domain属性,则用户
代理将 将 cookie 返回到源服务器。

因此,您所要做的就是在从 domain.com 设置 cookie 时省略域属性。

setcookie($name,$value,$expires,'/','');

为了进一步确认,我自己测试了它,我可以向您保证,cookie 无法从当您设置子域并忽略域属性时。

Of cource you can! That's what most websites do. Even the built-in php function session_start() does that. and its Set-Cookie http response header looks just as simple as this:

Set-Cookie: PHPSESSID=fe26eaac143ef75ffcbc91bbe5780d0d; path=/

According to RFC 6265, section 4.1.2.3, the last statement in the paragraph:

If the server omits the Domain attribute, the user
agent will return the cookie only to the origin server.

So, all you have to do is to omit the domain attribute while setting the cookie from your domain.com

setcookie($name,$value,$expires,'/','');

For further confirmation, I tested it myself, and I can assure you, cookies aren't accessible from subdomains when you set 'em while omitting the domain attribute.

划一舟意中人 2024-08-11 10:26:36

这是不可能的,因为 cookie 域与域名尾部匹配。你必须使用www。

It is not possible as the cookie domain is tail matched against the domain name. You will have to go with www.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文