PHP-Development

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 www.example.com, subdomain.example.com

 

Share and Enjoy

  • Facebook
  • Twitter
  • Google Plus
  • Live
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • Instapaper
  • FriendFeed