HowTo: Get Cookies Across Subdomains PHP

HowTo: Get Cookies Across Subdomains PHP

So you are using cookies on your website, but when a visitor visits and 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:

setcookie('YourCookieName', 'Some Values', time() + 3600, '/', '');

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

setcookie('YourCookieName', 'Some Values', time() + 3600, '/', '');

All done ^^ Enjoy

Posted by voidet

Categorised under PHP
Bookmark the permalink or leave a trackback.


  1. Aaron


    June 25, 2008 @ 7:27 am
  2. Nice tutorial

    October 31, 2008 @ 8:59 pm
  3. Great tut, straight to the point. Good work.

    April 23, 2009 @ 7:17 am
  4. Thanks. Very useful for sites with, etc.

    October 29, 2009 @ 4:12 am
  5. khal3d

    what about session?

    December 12, 2009 @ 10:03 am
    • VoiDeT

      ini_set(‘session.cookie_domain’, ‘’); should do the trick

      December 12, 2009 @ 10:09 am
  6. Hey thanks a lot for this post….this is exactly the problem I was having…the extra period fixed everything.

    November 21, 2010 @ 3:57 am
  7. and if you want to use your cookies with multiple domains just add this in your php file or php.ini file.

    ini_set(“session.cookie_domain”, “”);

    April 9, 2011 @ 4:23 am
  8. Excellent info once again! Thank you:)

    February 27, 2012 @ 11:04 pm

One Trackback

  1. [...] Get Cookies Across Subdomains in PHP [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Links:nike air max pas chernike air max pas chernike tn pas cherray ban pas chernike air max pas chernike tn pas cherray ban pas cherray ban pas cherray ban pas cher