Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Host and Domain in Cookie parameters PHP

Tags:

php

cookies

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

like image 913
Shakti Singh Avatar asked Jan 14 '11 07:01

Shakti Singh


People also ask

What is domain in cookie?

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.

Can cookie have multiple domains?

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.

Can cookie be set for another domain?

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.

Can domain set cookie for subdomain?

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.


1 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");
like image 79
Kel Avatar answered Oct 09 '22 19:10

Kel