使用 Rails 从子域读取域的 cookie

发布于 2024-09-15 20:39:01 字数 508 浏览 2 评论 0原文

为了存储一些 Google Analytics 数据,我想从我的 www 子域 (domain=www.example.com) 访问 GA“__utmz”域的 cookie (domain=.example.com)。 com)。是否可以从子域读取该域的 cookie?如果是,我该如何使用 Rails 做到这一点?

cookies[:__utmz] 似乎并不适用于所有浏览器。

我知道我可以配置我的应用程序,将 cookie 域设置为我的 production.rb 中的“.example.com”(config.action_controller.session = { :domain => ".example.com" } ),但我不想(因为我不希望我的 www 子域的 cookie 在所有子域之间共享)。

我希望我的问题足够清楚......

提前感谢您的帮助(并对我的语言中可能出现的错误表示歉意......)

In order to store some Google Analytics data, I would like to access to GA "__utmz" domain's cookie (domain=.example.com) from my www subdomain (domain=www.example.com). Is it possible to read this domain's cookie from a subdomain ? If yes, how can I do that with Rails ?

cookies[:__utmz] doesn't seem to work with all browsers.

I know I could configure my app setting the cookie domain to '.example.com' in my production.rb (config.action_controller.session = { :domain => ".example.com" }), but I'd rather not (because I don't want my www-subdomain's cookie to be shared among all subdomains).

I hope my question is clear enough...

Thanks by advance for your help (and sorry for the possible mistakes in my language...)

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

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

发布评论

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

评论(1

维持三分热 2024-09-22 20:39:01

允许 subdomain.example.com 上的应用程序从 www.example.com 读取 cookie 的唯一方法是让 www.example.com 设置顶级 example.com cookie。

这将允许 subdomain.example.com 读取它,但它也将允许 example.com 的所有其他子域看到它 - 您说过您不希望这样做。

为了遵循这一点 - cookie 按名称检索并由浏览器限定范围。如果有多个同名的cookie,就会发生冲突。我相信,如果 example.com 和 subdomain.example.com cookie 都存在,则更通用的 example.com cookie 将是为 subdomain.example.com 返回的唯一一个。

TL;DR

不要使用顶级域 cookie,除非您希望数据成为跨所有域的权威 cookie(例如单点登录)。如果您为 Google Analytics 执行此操作,您的不同子域将会发生冲突。

The only way to allow an app on subdomain.example.com to read a cookie from www.example.com would be for www.example.com to set a top-level example.com cookie.

This would allow subdomain.example.com to read it, but it would also allow every other subdomain of example.com to see this - which you said you don't want.

To follow this through - cookies are retrieved by name and scoped by the browser. If there are multiple cookies with the same name, you will have collisions. I believe the more generic example.com cookie will be the only one ever returned for subdomain.example.com if BOTH example.com and subdomain.example.com cookies exist.

TL;DR

Don't use top-level domain cookies unless you want the data to be the authoritative cookie across all domains (like single sign on). If you do this for Google Analytics you're going to collide on your different subdomains.

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