使用 PHP 或 JavaScript 在多个域上设置 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19531183/
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
Set cookie on multiple domains with PHP or JavaScript
提问by steveo225
I have 3 domains that are themed the same. If somebody chooses a different theme, I want it to propagate across all 3 domains so their experience stays the same.
我有 3 个主题相同的域。如果有人选择了不同的主题,我希望它在所有 3 个域中传播,以便他们的体验保持不变。
I was planning to accomplish this by setting a cookie on domain1, redirect to domain2 where the cookie is set, redirect to domain3 where the cookie is set, redirect back. Since the information doesn't need to be secure or protected, this will work fine with no real problems.
我打算通过在 domain1 上设置 cookie 来实现这一点,重定向到设置了 cookie 的 domain2,重定向到设置了 cookie 的 domain3,然后重定向回来。由于信息不需要安全或受保护,因此这将正常工作而不会出现实际问题。
I hate the idea 3 redirects just to set a cookie on each domain and was looking for something a little more elegant.
我讨厌想法 3 重定向只是为了在每个域上设置一个 cookie,并且正在寻找更优雅的东西。
回答by Subin
Do what Google is doing. Yes, Google is doing this same trick to login the user to YouTube and other Google services which are on different domains.
做谷歌正在做的事情。是的,Google 正在使用相同的技巧将用户登录到不同域上的 YouTube 和其他 Google 服务。
Create a PHPfile that sets the cookie on all 3 domains. Then on the domain where the theme is going to set, create a HTMLfile that would load the PHPfile that sets cookie on the other 2 domains. Example:
创建一个PHP文件,在所有 3 个域上设置 cookie。然后在要设置主题的域上,创建一个HTML文件,该文件将加载在其他 2 个域上设置 cookie的PHP文件。例子:
<html>
<head></head>
<body>
<p>Please wait.....</p>
<img src="http://domain2.com/setcookie.php?theme=whateveryourthemehere" />
<img src="http://domain3.com/setcookie.php?theme=whateveryourthemehere" />
</body>
</html>
Then add an onloadcallback on bodytag. The document will only load when the images completely load that is when cookies are set on the other 2 domains. Onload Callback :
然后在body标签上添加一个onload回调。该文档仅在图像完全加载时加载,即在其他 2 个域上设置 cookie 时。加载回调:
<head>
<script>
function loadComplete(){
window.location="http://domain1.com";//URL of domain1
}
</script>
</head>
<body onload="loadComplete()">
setcookie.php
设置cookie.php
We set the cookies on the other domains using a PHP file like this :
我们使用 PHP 文件在其他域上设置 cookie,如下所示:
<?php
if(isset($_GET['theme'])){
setcookie("theme", $_GET['theme'], time()+3600);
}
?>
Now cookies are set on the threedomains.
现在 cookie 设置在三个域上。
回答by Jason Wood
You could include the other two domains in iFrames. For more information search for "third party cookies". However depending on the browser settings, the user might not accept third party cookies. There are some other ideas here: What's your favorite cross domain cookie sharing approach?
您可以在 iFrame 中包含其他两个域。有关更多信息,请搜索“第三方 cookie”。但是,根据浏览器设置,用户可能不接受第三方 cookie。这里还有一些其他想法:您最喜欢的跨域 cookie 共享方法是什么?