Home » Php » Difference between Host and Domain in Cookie parameters PHP

Difference between Host and Domain in Cookie parameters PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&Answers:

According to comments to setcookie() function description, the difference is the following:

  • Host: www.example.com

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, "");
  • Domain: .www.example.com

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");