?
Solved

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

Posted on 2011-03-21
6
Medium Priority
?
752 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
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

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

801 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