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?
To read a cookie that is being shadowed by a cookie with a more-specific domain or path , the only thing you can do is load a page for which the more-specific cookie is out-of-scope, and read it from there. document. cookie= 'foo=;domain=sub.domain.example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT'; document.
A cookie is associated with a particular domain and scheme (such as http or https ), and may also be associated with subdomains if the Set-Cookie Domain attribute is set.
The default cookie domain value is the network domain of the computer you are installing the server on. The default cookie domain specifies the most restrictive access. This value is used to control cookie visibility between servers within the domain.
As you may know, cookie can't be set in a different domain from another domain directly. If you're having multiple sites in where you need to set a cookie from a parent site, you can use basic HTML and JS to set the cookies. Google is using this same way.
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");
If I understand you correctly you want to change the domain of the cookies currently existing on the clients?
This is not possible(*).
When getting a cookie server side, is it possible for you to see if it was set for the www domain, keeping in mind that the cookie passed form the client has no domain information?
(*) It might be possible with JavaScript on the client side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With