HowTo: Get Cookies Across Subdomains PHP

Meta: April 08th 2008 // PHP // 4,563 views

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:

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:

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

All done ^^ Enjoy

WeDecal.com

Postscript: Leave A Comment // Subscribe (RSS Feed)

Comments About HowTo: Get Cookies Across Subdomains PHP

// 6 comments so far.

  1. Aaron // June 25th 2008

    Thanks!

  2. Kenneth // October 31st 2008

    Nice tutorial

  3. Biily Bob // April 23rd 2009

    Great tut, straight to the point. Good work.

  4. SpYeR // October 29th 2009

    Thanks. Very useful for sites with blogs.mydomain.com, news.mydomain.com etc.

  5. khal3d // December 12th 2009

    what about session?

  6. VoiDeT // December 12th 2009

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

Who Are You?

Your Email Address

Your Website

:D :) :o :eek: :( :lol: :wink: :arrow: :idea: :?: :!: :evil: :p

You can follow any responses to this entry via its RSS comments feed. You may also leave a trackback by clicking this link.