javascript 我如何设置第三方 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12514222/
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
How I can set third party cookie
提问by Alok
How I can set third party cookie. I am having requirement set cookie and cookie will be enable d in visited websites, Like I have set cookie in abc.com when I visit cde.com or def.com or ghi.com so set cookie will be fetch on all the website. How I can fetch cookie on all domain in javascript.
我如何设置第三方 cookie。我需要设置 cookie,并且 cookie 将在访问过的网站中启用,就像我在访问 cde.com 或 def.com 或 ghi.com 时在 abc.com 中设置了 cookie 一样,所以设置的 cookie 将在所有网站上获取。我如何在 javascript 中获取所有域上的 cookie。
回答by rsp
It would be possible to directly share cookies if instead of abc.com, cde.com, def.com, you would have abc.xyz.com, cde.xyz.com, def.xyz.com, (google for subdomain cookies). Maybe it is possible to set your websites like that and still meet your requirements.
如果您拥有 abc.xyz.com、cde.xyz.com、def.xyz.com,而不是 abc.com、cde.com、def.com,则可以直接共享 cookie(谷歌搜索子域 cookie) . 也许可以像这样设置您的网站并且仍然满足您的要求。
Otherwise, if all of those websites cannot be in subdomains of the same domain, then you may have one of them act as a central cookie server and when the user is on other domains you could use JSONP to direct them to some script on your cookie domain that would send you their id or whatever and make your script that handles the AJAX request set its domain cookie to the same value. Example:
否则,如果所有这些网站都不能在同一域的子域中,那么您可以让其中一个充当中央 cookie 服务器,当用户在其他域上时,您可以使用 JSONP 将他们定向到 cookie 上的某个脚本将向您发送他们的 id 或其他任何内容的域,并使处理 AJAX 请求的脚本将其域 cookie 设置为相同的值。例子:
- user visits def.com
- JavaScript code on def.com makes an JSONP request to abc.com
- abc.com sets a cookie if it is not set yet
- abc.com returns the cookie value as a response to the script on def.com
- script on def.com sets its local def.com cookie to the same value
- 用户访问 def.com
- def.com 上的 JavaScript 代码向 abc.com 发出 JSONP 请求
- 如果尚未设置,abc.com 会设置 cookie
- abc.com 返回 cookie 值作为对 def.com 上脚本的响应
- def.com 上的脚本将其本地 def.com cookie 设置为相同的值
and now your servers can coordinate their statistics etc.
现在你的服务器可以协调他们的统计数据等。
All of this is of course possible only if all of the websites cooperate with each other, ie. your websites cannot mess with cookies of other websites that you don't control as well.
所有这一切当然只有在所有网站相互合作时才有可能,即。您的网站不能与您无法控制的其他网站的 cookie 混淆。
UPDATE:
更新:
See also Breaking The Cross Domain Barriertalk by Alex Sexton for some inspirations and code example.
另请参阅Alex Sexton 的Breaking The Cross Domain Barrier演讲以获得一些灵感和代码示例。
UPDATE:
更新:
If you decide to use a method similar to the one outlined above, make sure you understand the potential security issueslike a possibility of a cross site request forgeryattack. Search Stack Overflow for JSONP securityto find more informations on how to make it safe. Keep in mind that the above explanation is a simplification of a somewhat complicated process that you need to understand. You have been warned.
如果您决定使用类似于上述方法的方法,请确保您了解潜在的安全问题,例如跨站点请求伪造攻击的可能性。搜索 Stack Overflow 以获取 JSONP 安全性,以查找有关如何使其安全的更多信息。请记住,上面的解释是对您需要理解的稍微复杂的过程的简化。你被警告了。
回答by Aurelio De Rosa
You can only access your domain cookies.
您只能访问您的域 cookie。
回答by QQping
It is not possible to assign multi-domain cookies.
无法分配多域 cookie。