egoselfaxis
asked on
Need to get parent domain name using PHP (that excludes the subdomain prefix)
I need a simple php script that first gets the hostname using $_SERVER['HTTP_HOST'], and that then extracts the parent domain from that url.
For example, .. if the HTTP_HOST value is "clients.mywebsite.com", .. I need to extract the "mywebsite.com" domain from it and store it in a variable. Please advise.
Thanks!
- Yvan
For example, .. if the HTTP_HOST value is "clients.mywebsite.com", .. I need to extract the "mywebsite.com" domain from it and store it in a variable. Please advise.
Thanks!
- Yvan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nevermind guys .. I figured out a simpler way to do it:
list($sd, $pd, $ds) = split('[.]', $_SERVER['HTTP_HOST']);
$thedomain = "http://" . $pd . "." . $ds;
I'll just award the points to you guys anyways -- thanks for your efforts!
- Yvan
list($sd, $pd, $ds) = split('[.]', $_SERVER['HTTP_HOST']);
$thedomain = "http://" . $pd . "." . $ds;
I'll just award the points to you guys anyways -- thanks for your efforts!
- Yvan
Hi again,
sorry, I couldn't test my script at work and forgot the dollar mark before the "intDomParts" in line 3.
Your solution has the drawback that it splits the domain name beginning from the start of the string instead of the end, in case of subdomains with more than one part ("login.customer.mydomain. com"), the generated domain name would be quite useless.
To be honest, it all depends on where your script will be used, if your company only has one subdomain level, your chosen script will work absolutely fine ;-)
Thanks for giving assist points!
sorry, I couldn't test my script at work and forgot the dollar mark before the "intDomParts" in line 3.
Your solution has the drawback that it splits the domain name beginning from the start of the string instead of the end, in case of subdomains with more than one part ("login.customer.mydomain.
To be honest, it all depends on where your script will be used, if your company only has one subdomain level, your chosen script will work absolutely fine ;-)
Thanks for giving assist points!
ASKER
Yes -- we only deal with 3rd level domains (single subodmain prefixes).
- yg
- yg
ASKER
$domainArray= explode('.', $_SERVER['HTTP_HOST']);
$intDomParts= count($domainArray);
$domainName= $domainArray[intDomParts-2
echo $domainName;