Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 214
lvil Avatar asked Oct 27 '11 08:10

lvil


People also ask

How do you find the domain of a cookie?

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.

Is cookie domain specific?

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.

What is the default domain for a cookie?

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.

Can cookie have 2 domains?

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.


2 Answers

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");
like image 185
Noodles Avatar answered Oct 26 '22 05:10

Noodles


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.

like image 40
Roger Lindsjö Avatar answered Oct 26 '22 06:10

Roger Lindsjö