Set Cookies From Different Domains

Set Cookies From Different Domains

If you manage multiple domains , but need to share any cookies value across the domains , this might be great starting point to set cookies of a domain from a different domain. Later you can call that cookie value from respective domain. Think about the scenario MSN setting the login status cookie on the various domains of theirs (Hotmail, live.com, msn.com). One thing you have keep in mind , cookie can’t be set in a different domain from another domain directly. But there is workaround for issue and the workaround is quite easy than you think.

Suppose You have three site

On this tutorial , We will set cookies on example1.com and example2.com from zhostz.com

Now on other two domains (example1.com and example2.com) we will need a php or any server side script which is capable to create cookies on those domains. In this tutorial create a php file name cookies.php

In the above case we’re not mentioning the path or domain because PHP automatically sets it.

On the main domain (zhostz.com) from where we are’re going to set the cookies, all need to call that cookies.php page through img tag . Once the page load of the main domain is completed , cookies will be set on those two domains.

<img src=”http://www.example2.com/cookies.php” style=”display:none;” />

To know when page load is completed and cookies are setted, you can use event listener on the document, so that we will know when the cookies are set completely. This is equal to document loaded listener because when the images are loaded, cookies are set.

On the main domain (example.com) where you’re going to ask the other two domains to set the cookie, create an HTML page with the following content :

When the client visits the above page, a page is requested from the mysite.com domain as image source, but the page is not an image. This page that is on the other domain will set the cookie on that domain.

You can also send data to the other domains as GET parameters so that cookies based on that data can be created. But, when you send passwords or other secure content, be sure to encrypt the string.

468 ad

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">