[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-03-21
6
Medium Priority
?
763 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 1000 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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