HowTo: Get Cookies Across Subdomains PHP

So you are using cookies on your website, but when a visitor visits www.yourdomain.com and yourdomain.com the cookie doesn't get set across both! That is because in essence, www. is simply a subdomain. The "www" component is not a protocol, it is not necessary, instead all it is, is just a marketing ploy. But anyway. Here is what you would do normally:

  1. setcookie('YourCookieName', 'Some Values', time() + 3600, '/', 'mydomain.com');

Damn! Almost there! The part you are missing to add a cookie irrespective of the subdomain is ".mydomain.com". Yep, just add a . infront of your domain name. So you would have:

  1. setcookie('YourCookieName', 'Some Values', time() + 3600, '/', '.mydomain.com');

All done ^^ Enjoy

  1. 2 Responses to “HowTo: Get Cookies Across Subdomains PHP”

  2. By Aaron UNITED STATES Mac OS X Safari 525.20 on Jun 25, 2008

    Thanks!

  3. By Kenneth GERMANY Windows XP Mozilla Firefox 3.0.3 on Oct 31, 2008

    Nice tutorial

Post a Comment