Solved

Need to get parent domain name using PHP (that excludes the subdomain prefix)

Posted on 2011-03-21
6
732 Views
Last Modified: 2012-05-11
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
Comment
Question by:egoselfaxis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
dtrance earned 250 total points
ID: 35182475
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
 
LVL 7

Assisted Solution

by:Swafnil
Swafnil earned 250 total points
ID: 35182529
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
 

Author Comment

by:egoselfaxis
ID: 35182890
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
Technology Partners: 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!

 

Author Comment

by:egoselfaxis
ID: 35183275
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
 
LVL 7

Expert Comment

by:Swafnil
ID: 35183347
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
 

Author Comment

by:egoselfaxis
ID: 35183374
Yes -- we only deal with 3rd level domains (single subodmain prefixes).

- yg
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question