J
J
Jawell2018-01-09 12:12:10
PHP
Jawell, 2018-01-09 12:12:10

How to set cookies ONLY on the main domain?

When setting cookies:
setcookie("Data", $data, 0, "/", "example.com");
they still extend to subdomains. How can I make sure that these cookies are only on the main domain?
Note:
In RFC 2109, a domain without a leading dot meant that it could not be used on subdomains, and only a leading dot ( .mydomain.com ) would allow it to be used on subdomains.
However, modern browsers respect the new RFC 6265 specification and ignore any leading dot, which means you can use a cookie on subdomains as well as top level domains.
So if you set a cookie like the second example above from mydomain .com , it will be available for subdomain.mydomain.com and vice versa.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kalibrov, 2018-01-09
@Jawell

do not specify domain at all in setcookie, then it will not work on subdomains:
setcookie("Data", $data, 1, "/");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question