简体   繁体   English

使用Rails从子域读取域的cookie

[英]Read domain's cookie from subdomain with Rails

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 ). 为了存储一些Google Analytics数据,我想从我的www子域( domain=www.example.com )访问GA“__utmz”域名的cookie( domain=.example.com )。 Is it possible to read this domain's cookie from a subdomain ? 是否可以从子域读取此域的cookie? If yes, how can I do that with Rails ? 如果是的话,我怎么能用Rails做到这一点?

cookies[:__utmz] doesn't seem to work with all browsers. cookies[:__utmz]似乎不适用于所有浏览器。

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

I hope my question is clear enough... 我希望我的问题足够清楚......

Thanks by advance for your help (and sorry for the possible mistakes in my language...) 感谢您的帮助(对不起我的语言可能出错......)

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. 允许subdomain.example.com上的应用从www.example.com读取Cookie的唯一方法是让www.example.com设置顶级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. 这将允许subdomain.example.com读取它,但它也会允许example.com的每个其他子域看到这个 - 你说你不想要它。

To follow this through - cookies are retrieved by name and scoped by the browser. 要遵循这一点 - 通过名称检索cookie并由浏览器确定范围。 If there are multiple cookies with the same name, you will have collisions. 如果有多个具有相同名称的cookie,则会发生冲突。 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. 我相信如果存在BOTH example.com和subdomain.example.com cookie,则更通用的example.com cookie将是subdomain.example.com唯一返回的cookie。

TL;DR 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). 除非您希望数据是所有域中的权威cookie(例如单点登录),否则请勿使用顶级域Cookie。 If you do this for Google Analytics you're going to collide on your different subdomains. 如果您为Google Analytics执行此操作,则会在您的不同子域上发生冲突。

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

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