• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

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
0
egoselfaxis
Asked:
egoselfaxis
  • 3
  • 2
2 Solutions
 
dtranceCommented:
Try this:
function gethost() {
  static $host;

  if(!isset($host)) {
    global $base_url;
    $parts = parse_url($base_url);
    $host = ($parts['host']);
  }
  return $host;
}

function getdomain() {
  static $domain;

  if(!isset($domain)) {
    $host_parts = explode('.', gethost());
    $host_parts = array_reverse($host_parts);
    $domain = $host_parts[1] . '.' . $host_parts[0];
  }

  return $domain;
}
0
 
SwafnilCommented:
The answer could be as simple as:
$domainArray= explode('.', $_SERVER['HTTP_HOST']);
$intDomParts= count($domainArray);
$domainName= $domainArray[intDomParts-2]. '.' .  $domainArray[intDomParts-1];

Open in new window


... BUT ...

In fact there is no simple solution to your question because some of the international domains do not follow the rules of domain names, i.e. the english "co.uk", the given function would simply return a "co.uk" as domain name (which is in fact true, the "uk" part is the TLD, the "co" just the domain name).

Hope that'll get you started ;)
0
 
egoselfaxisAuthor Commented:
Swafni -- your script does not work for me -- it only returns and displays a period (.):

$domainArray= explode('.', $_SERVER['HTTP_HOST']);
$intDomParts= count($domainArray);
$domainName= $domainArray[intDomParts-2]. '.' .  $domainArray[intDomParts-1];

echo $domainName;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
egoselfaxisAuthor Commented:
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
0
 
SwafnilCommented:
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!
0
 
egoselfaxisAuthor Commented:
Yes -- we only deal with 3rd level domains (single subodmain prefixes).

- yg
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now