Suppose I have two session cookies which looks like
First one
Name: d58ba4091c622661a0d46f03b412ac8b
Content: m9ciub2u3ig59638r43uqjb8e6
Host: www.example.com
Path: /
Send for: Any type of connection
Expires:Sunday, February 27, 2011 5:50:18 PM
and second one
Name: test
Content: kdfssdfb2ufdfjww3436detasd
Domain: .www.example.com
Path: /
Send for: Any type of connection
Expires:Sunday, February 27, 2011 5:50:18 PM
If you can see first one contain Host parameter
and value and second one contain the Domain
parameter.
What is exact they do and what's the difference b/w them?
Thanks
The Domain attribute specifies which hosts can receive a cookie. If unspecified, the attribute defaults to the same host that set the cookie, excluding subdomains. If Domain is specified, then subdomains are always included. Therefore, specifying Domain is less restrictive than omitting it.
A cookie can only have 1 domain value. So you can either set it to a specific domain or the top domain wich includes all the subdomains.
Cookies that are stored and accessed under a specific domain cannot be accessed from a page hosted on another domain. Therefore, the cookie data has to be passed along when leaving one domain and going to the other one.
Please everyone note that you can set a cookie from a subdomain on a domain. But you CAN'T set a cookie from a domain on a subdomain.
According to comments to setcookie() function description, the difference is the following:
is restricted to specified host, so this cookie will not be visible neither to entirely different domains, nor to subdomains. Such cookie is created if setcookie() parameter $domain is set to empty string:
setcookie($name, $value, time()+3600, $path, "");
is restricted to specified domain, so this cookie will be visible to subdomains of specified domain (all domains like *.www.example.com). Such cookie is created if setcookie() parameter $domain is set to some domain:
setcookie($name, $value, time()+3600, $path, "www.example.com");
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