Use of the same cookie in selected subdomains

I am trying to find a way to share cookies across multiple subdomains.

Setting cookie as:

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

does just that. But there is a small problem. This will split cookies across all subdomains.

My problem is that I have other environments (Dev and test) configured on 2 subdomains. I am looking for a way to share cookies through "custom" subdomains. that is, a share in some subdomains, not a share of others. I am not sure if something like this exists.

Any help is appreciated. Thanks.

+6
source share
3 answers

After much thought and study of this and reading of all the valuable comments posted above, I think there is no easy solution to this.

I could go with the solution provided by Adrien Hinger, but that would mean an extra check every time the user arrives.

I assume that I have no choice but to migrate the development environment and test environments to another domain.

Many thanks to all of you guys for your thoughts.

0
source

As far as I know, you can share all subdomains using ".mydomain.com" (how you do it), or you need to be specific and configure only one subdomain using, for example, "test.mydomain.com".

You can also use some tricks or workarounds, for example, prefixing the cookie name and then executing the logical server, but I'm not sure if this is the solution you are looking for.

+1
source

The domain=.example.com attribute specifically makes cookies available for all subdomains. Just drop this attribute and the cookie can only be read by the subdomain that set it.

It is easy.

0
source

Source: https://habr.com/ru/post/922713/


All Articles