简体   繁体   中英

Cookies conflict in domain - subdomain

We have a website www.example.com that use some cookies. And we have a copy of this website in a subfolder like these:

www.example.com:8000/test/

The copy of this subfolder is for testing.

We create the cookies like this (for example) in the domain and in the subfolder:

setcookie("name", "value", time() + 1800);

And when we navigate into domain and subfolder on different tabs into the same navigator we have conflict between cookies because we have the same cookies in both.

I think that we can do this in the subfolder:

setcookie("name", "value", time() + 1800, "test", "www.example.com:8000");

To my knowledge this creates only the cookies in the subfolder and one part of the problem would be solved. But, to my knowledge, when we create a cookie in the root of the domain, this cookie is also available in the subfolders... then the problem will continue...

Anyone knows how can I define a cookie only for the root folder of the domain?

Thanks a lot.

You can't

Cookies are strongly connected with website. A domain to be specific. And that's why they are great and powerfull. Any cookie defined anywhere within www.example.com will be accessible in any page on this domain. But on the www.blog.example.com however, that cookie will not be accessible, as subdomains are technically separate domains/websites. Folders and subdirectiories are not.

Using subfolder allows you to set cookie to more deep parts of website. But you cannot limit cookie to root only.

How to help you

Use different names for your cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM