Set cookie on multiple domains with PHP or JavaScript

前端 未结 2 1334
小鲜肉
小鲜肉 2020-12-02 14:41

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.

I w

相关标签:
2条回答
  • 2020-12-02 15:27

    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?

    0 讨论(0)
  • 2020-12-02 15:32

    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.

    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>
    

    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

    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);
    }
    ?>
    

    Now cookies are set on the three domains.

    Source - My Blog

    0 讨论(0)
提交回复
热议问题