使用PHP或JavaScript在多个域中设置Cookie [英] Set cookie on multiple domains with PHP or JavaScript

查看:149
本文介绍了使用PHP或JavaScript在多个域中设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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>

然后在正文上添加 onload 标签。该文档只会在图片完全加载时加载,即在其他2个域上设置Cookie。 Onload回调:

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



其他域上的Cookie使用如下的PHP文件:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆