Skip to content
Advertisement

How to get the domain of a specific cookie?

There is a web site www.example.com
All cookies are set to the www subdomain.
Now there is a new subdomain and I want the cookies to be seen for all subdomains.

The goal is to rewrite the www.example.com cookies for all the old visitors to be .example.com or to write new ones for .example.com if set for www.

For this I want to get the domain of the existing cookies.
Is it possible? Is there a php function for this purpose?

Advertisement

Answer

I don’t think the domain is available when reading cookies, this is limited by the browser. A solution would be to remove the old cookie and change it to the new domain.

E.g.

$value = $_COOKIE['TestCookie'];
setcookie("TestCookie", "", time() - 3600, "www.example.com");
setcookie("TestCookie", $value, time + (60 * 60 * 24 * 30), ".example.com");
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement