問題描述
我有 3 個主題相同的域.如果有人選擇了不同的主題,我希望它在所有 3 個域中傳播,以便他們的體驗保持不變.
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.
我打算通過在 domain1 上設置 cookie 來實現這一點,重定向到設置了 cookie 的 domain2,重定向到設置了 cookie 的 domain3,然后重定向回來.由于信息不需要安全或受保護,因此這將正常工作,不會出現實際問題.
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.
我討厭 3 重定向只是為了在每個域上設置一個 cookie 并且正在尋找更優雅的東西.
I hate the idea 3 redirects just to set a cookie on each domain and was looking for something a little more elegant.
推薦答案
做 Google 正在做的事情.是的,Google 正在使用相同的技巧讓用戶登錄 YouTube 和不同域中的其他 Google 服務.
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.
創建一個 PHP 文件,在所有 3 個域上設置 cookie.然后在要設置主題的域上,創建一個 HTML 文件,該文件將加載在其他 2 個域上設置 cookie 的 PHP 文件.示例:
Create a PHP file that sets the cookie on all 3 domains. Then on the domain where the theme is going to set, create a HTML file that would load the PHP file that sets cookie on the other 2 domains. Example:
<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>
然后在 body 標簽上添加一個 onload 回調.該文檔僅在圖像完全加載時加載,即在其他 2 個域上設置 cookie 時.加載回調:
Then add an onload callback on body tag. The document will only load when the images completely load that is when cookies are set on the other 2 domains. Onload Callback :
<head>
<script>
function loadComplete(){
window.location="http://domain1.com";//URL of domain1
}
</script>
</head>
<body onload="loadComplete()">
setcookie.php
我們使用 PHP 文件在其他域上設置 cookie,如下所示:
setcookie.php
We set the cookies on the other domains using a PHP file like this :
<?php
if(isset($_GET['theme'])){
setcookie("theme", $_GET['theme'], time()+3600);
}
?>
現在 cookie 設置在三個域上.
Now cookies are set on the three domains.
來源 - 我的博客
這篇關于使用 PHP 或 JavaScript 在多個域上設置 cookie的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!