0

I have a website with two domain names which shows the same content from both domain names and it is also correct for sub-domain, but the problem is when I set a cookie for this website which is used in its sub-domain websites.

The cookie is being set only to one domain name, not for both. What is the problem?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Somesh
  • 121
  • 8
  • you cannot set cookie for 2 different domains – zerkms Aug 31 '12 at 05:22
  • 1
    Imagine being able to create a cookie in your domain and use it in facebook...boy, wouldnt that be something! ;-) – raidenace Aug 31 '12 at 05:26
  • @Raidenace : Plz read the question correctly i said same website not two different website ,but that website has multiple domain names and this domain names share same folder and pages, at his time if we set the cookie it should effect in both domain names, because we are setting the cookie in login page which is used by both.. – Somesh Aug 31 '12 at 08:48

4 Answers4

1

As you must know, a cookie can only be set for a domain from that domain (including its subdomains). And if your domains do not share a common superdomain, you need set each cookie for each domain separately.

You can do this with a script that on each domain that sets the cookie for you. But make sure to authenticate requests to these scripts so that only you can set the cookies.

Refer link

Vinay
  • 2,564
  • 4
  • 26
  • 35
  • Both Domain names share same folder and same pages and also the sub-domains , at this time if make changes to the content it get's effected in both domain name ,but this is not working same for the cookie. why?? – Somesh Aug 31 '12 at 08:40
  • Cookies are not like sessions, they are stored on client-side. so whenever you creating a cookie for one domain, it never going to share by other domain. Though it share same folder and same pages it cant share cookie variables. Solution would be to write a script which sets cookie and use some authentication mechanism. – Vinay Aug 31 '12 at 09:03
  • I don't want to share the cookie between both, i want to create,but it is only creating for one domain-name not for the other name as it should be created for other one also as it uses same page of login in which i am creating the cookie.. – Somesh Aug 31 '12 at 10:11
  • You can use one solution, you first [retrieve siteURL](http://stackoverflow.com/questions/4503135/php-get-site-url-protocol-http-vs-https), then accordingly create cookie for differnet domain-name using if-else. and when you have to verify that cookiename, again retrieve siteURL use if-else, and perform whatever action you want. – Vinay Aug 31 '12 at 10:21
0

You can setup an API on a common domain to set cookies for all domains which want to access said cookie info. The common domain cookie would have namespace keys representing the domains, etc. and would do all the cookie reading/writing. Use XHR to access the common domain with params you wish to be placed into the common cookie. Just keep in mind Safari disables 3rd party cookies by default.

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
0

You cannot share cookies between two different domains, even if you own both of them.


SO has some posts regarding cross domain cookies, and other possible solutions:

Cross domain cookies

Cross-Domain Cookies

Community
  • 1
  • 1
Charlie
  • 11,380
  • 19
  • 83
  • 138
0

Cookies are not designed to be accessible for other domains But there is always a workaround ;) There are to method to achieve this

  1. including 2 hidden iframes from different domains to set cookies with same value.

    Ex. http://productforums.google.com/forum/#!topic/websiteoptimizer/aD4rZSoaKNo

  2. using master and slave domain configuration

    Example:

    https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite

    http://www.codeguru.com/csharp/csharp/cs_internet/article.php/c19417/Sharing-Cookies-Across-Domains.htm

GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
  • it is the one website with two different names for example "example.com" and "exam.com" they both use same pages so i set the cookie in login page but seting is working for "example.com" and not for "exam.com" – Somesh Aug 31 '12 at 07:21
  • I have edited my answer with some references please give it a try – GajendraSinghParihar Aug 31 '12 at 07:36
  • but Both Domain names share same folder and same pages and also the sub-domains , at this time if make changes to the content it get's effected in both domain name ,but this is not working same for the cookie. why?? – Somesh Aug 31 '12 at 08:52
  • its not working for cookies because of browser will serve cookies with same domain of page . – GajendraSinghParihar Aug 31 '12 at 09:00
  • to your code to work create a pages say `foo.php` to set cookie and get cookie now include 2 iframe different domain origan (`domainone.com/foo.php` and `domaintwo.com/foo.php`) to same page of yours and pass the encoded argument to create cookies to both iframe – GajendraSinghParihar Aug 31 '12 at 09:03
  • I want to create cookie ,but it is only creating for one domain-name not for the other name as it should be created for other one also as it uses same page of login in which i am creating the cookie. – Somesh Aug 31 '12 at 12:19