cookie and subdomains


A cookie can be set either for a specific domain or a domain and its subdomain.

Cookie for a specific domain:
When you set a cookie from a web server in php using setcookie() function then this function accepts some parameters. Some parameters are required and some are optional. In those parameters there is one parameter called domain, which means this cookie would be available for that domain. Domain parameter is an optional parameter.

If you don’t set the domain in setcookie() function then it will set the cookie which would be available to current host only. It won’t be available its subdomain.

setcookie("TestCookie", $value, time()+3600);

Cookie for domain and subdomains:
If you set the domain (top level domain) name in setcookie() function then it will set the cookie which would be availabe to its domain and subdomains.
For ex.

setcookie("TestCookie", $value, time()+3600,'example.com');

Above function will set the cookie with the domain “.example.com”. This cookie would be available to all its subdomains like http://www.example.com, subdomain.example.com

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s