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 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.
I hate the idea 3 redirects just to set a cookie on each domain and was looking for something a little more elegant.
As you may know, cookie can't be set in a different domain from another domain directly. If you're having multiple sites in where you need to set a cookie from a parent site, you can use basic HTML and JS to set the cookies. Google is using this same way.
You cannot set cookies for another domain.
JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page. How PHP cookies work? As you can see, the Set-Cookie header contains a name-value pair, a GMT date, a path and a domain.
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()">
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With