Solved

$_SERVER["SERVER_NAME"] to get domain only

Posted on 2012-04-03
8
353 Views
Last Modified: 2012-08-13
I need to get the "mydomain.com" string without the subdomain "www" or "anysudbomain"

$_SERVER["SERVER_NAME"]

Open in new window

0
Comment
Question by:glynco
[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
8 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37799687
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 37799981
function GetDomain($url) { $nowww = ereg_replace('www\.','',$url); $domain = parse_url($nowww); if(!empty($domain["host"]))     {      return $domain["host"];      } else      {      return $domain["path"];      }   } 

Open in new window



refer for more details
http://corpocrat.com/2009/02/28/php-how-to-get-domain-hostname-from-url/
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37800123
What about using $_SERVER['SERVER_ADDR'] or localhost or 127.0.0.1 ? In conjunction with $_SERVER['SERVER_PORT']?

Bye, Olaf.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37800792
I would avoid the use of ereg() functions because they are deprecated and will be going away soon.  See the large red warning label here:
http://us2.php.net/manual/en/function.ereg-replace.php 

The general design of what you are looking for is the same information you need to set a cross-subdomain cookie (for example, a cookie that works with both test.twitter.com and docs.twitter.com).  This is an excerpt from my teaching example about cookies.  It should give you what you want.
    // MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS).
    $x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
    $y = count($x);
    if ($y == 1) // MAYBE 'localhost'?
    {
        $cookie_domain = $x[0];
    }
    else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
    {
        // CHOOSE THE LAST TWO POSITIONS 
        $cookie_domain = $x[$y-2] . '.' . $x[$y-1];
    }

Open in new window

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37802707
// CHOOSE THE LAST TWO POSITIONS 
        $cookie_domain = $x[$y-2] . '.' . $x[$y-1];

Open in new window


Which wouldn't work for co.uk

I'm still curious about localhost. Will or won't it also do?

Bye, Olaf.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37807878
Good point about .co.uk - perhaps the right answer is to know in advance what the subdomains are and remove them "manually" from the URL.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37815597
Additional to $_SERVER["SERVER_NAME"] you could try out

$host = `host 127.0.0.1`;
print_r($host);

Open in new window


It might just give you the computername though, not the domain.

And another way would be a central file to inlcude hardcoding the domain as in $domain = 'mydomain.com';

Bye, Olaf.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37816187
Here is the issue with trying to automate the process with a generalized solution.  Subdomain names may or may not be omitted.  Subdomain names may or may not contain dots.  TLDs that are one string only, like .com or .net work well with the algorithm I posted, as does localhost.  Perhaps one way to handle this would be to extract the TLD first.  You can get the list of TLDs from IANA.
http://data.iana.org/TLD/tlds-alpha-by-domain.txt

If you remove all the possible TLDs one at a time from the end of the URL you will turn something like domain.co.uk into domain and www.domain.co.uk into www.domain.  At that point it is a reasonable assumption that anything to the left of the first dot is a subdomain and it can be eliminated.  And if there is no dot left, there is no subdomain.  This will, of course, screw a site with a name like www.asia.com but that is not your domain anyway.

Conclusion:  If the algorithm I posted works for you, feel free to use it - it's got limitations, but it's satisfactory for all of my work.  If you want a finer level of control, this might be the best answer:
define('DOMAIN', 'mydomain.com');

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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