0

I would like to share a cookie across 2 domains as my mobile site runns on a subdomain.

production server:

www.server.com
m.server.com

development server:

rabbit.server
rabbit.m.server

My PHP-code to set the cookie looks like this:

if ($settings['development'] == true) // intranet does not work with subdomains :-(
    setcookie($cookiename,$sessid, $expires,'/','',0);  
else // production
    setcookie($cookiename,$sessid, $expires,'/', $subdomain.'.'.$domain['name'],0); 

How could I share this cookie across the 2 domains in order to have the client loged in on both sites?

merlin
  • 2,130
  • 3
  • 18
  • 38

3 Answers3

0

Is this what you mean?

"To make the cookie available to the whole domain (including all subdomains of it), simply set the value to the domain name ('example.com', in this case)."

http://php.net/manual/en/function.setcookie.php

markdwhite
  • 2,163
  • 16
  • 21
0

You dont have to explicitly define the sub-domain:

setcookie('cookiename','cookievalue',time()+(3600*24),'/');

Place cookie in root and it would be accessible every where.

So basically '/' defines that it can be accessed in all the folders.

Fakhruddin Ujjainwala
  • 2,313
  • 15
  • 25
0

Well, there's two ways of doing this.

You can either set the cookie on the whole domain, which will allow you to access it from any subdomain, or if you wish to only allow certain subdomains then you'll have to create two cookies, one for each.

You can't have one single cookie for two different subdomains only, you can enable it on the whole domain, or you can have multiple cookies, one for each subdomain.

Code-wise you have to change

setcookie($cookiename,$sessid, $expires,'/', $subdomain.'.'.$domain['name'],0);

to

setcookie($cookiename,$sessid, $expires,'/','.'.$domain['name'],0);
Cârnăciov
  • 1,079
  • 1
  • 10
  • 21