Separating Subdomain from Domain and TLD
Posted: Mon Jul 27, 2009 4:00 pm
I'm trying to set up sessions to work across subdomains. It is simple to use ini_set('session.cookie_domain', "$domain.$tld") when you know the domain. And here is the code I'm using to take care of it dynamically:
But what if I need the code to detect multi-part top-level domains? For instance, I'm hoping to have the following results:
"domain.com" : set ini to ".domain.com"
"sub.domain.com" : set ini to ".domain.com"
"sub.sub.domain.com" : set ini to ".domain.com"
"domain.ca.us.gov" : set ini to ".domain.ca.us.gov"
"sub.domain.ca.us.gov" : set ini to ".domain.ca.us.gov"
"localhost" : do not set ini
It seems like Apache would know the difference between subdomain, domain and TLD. Does it? And if so, how can I find out from php?
Code: Select all
if (preg_match('/[^\.]+\.[a-z]{2,6}$/i', $_SERVER['HTTP_HOST'], $match)) {
// set cookie to work across all subdomains
ini_set('session.cookie_domain', '.' . $match[0]);
} "domain.com" : set ini to ".domain.com"
"sub.domain.com" : set ini to ".domain.com"
"sub.sub.domain.com" : set ini to ".domain.com"
"domain.ca.us.gov" : set ini to ".domain.ca.us.gov"
"sub.domain.ca.us.gov" : set ini to ".domain.ca.us.gov"
"localhost" : do not set ini
It seems like Apache would know the difference between subdomain, domain and TLD. Does it? And if so, how can I find out from php?