Solved

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

Posted on 2011-03-21
6
691 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now