Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using same cookie across selective subdomains

I have been trying to find out a way to share cookies across multiple subdomains.

Setting the cookie like:

setcookie('token', base64_encode(serialize($token)), time()+10800, '/', '.mydomain.com');

does exactly that. But there is a slight problem here. This will share the cookie across all subdomains.

My problem is that I have other environments (Dev and test) set up on 2 subdomains. I am looking for a way to share cookies across "Selective" subdomains. i.e. share across some subdomains, and not share amongst others. I am not sure if anything like this exists.

Any help is appreciated. Thanks.

like image 504
Vishesh Joshi Avatar asked Aug 13 '12 10:08

Vishesh Joshi


People also ask

Can cookies be shared across sub domains?

Please everyone note that you can set a cookie from a subdomain on a domain. But you CAN'T set a cookie from a domain on a subdomain.

Is subdomain first party cookie?

Seems to have worked, so ASP.NET session cookies on different subdomains still count as first party. A cookie set on a website that is loaded in an iframe of a different website is considered to be a third party cookie to the parent website.

What is a host only cookie?

Host Only cookie means that the cookie should be handled by the browser to the server only to the same host/server that firstly sent it to the browser.


1 Answers

As far as I'm aware you can either share across all subdomains using '.mydomain.com' (as you are doing) or you have to be specific and target only one subdomain using, for example, 'test.mydomain.com'.

You can also use some tricks, or workarounds, like prefixing the cookie name and then doing the logic server side, but I'm not sure if this si the solution you are looking for.

like image 67
Adrien Hingert Avatar answered Nov 25 '22 07:11

Adrien Hingert