Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

$_SERVER["SERVER_NAME"] to get domain only

Posted on 2012-04-03
8
Medium Priority
?
360 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 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 30

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
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.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 30

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 111

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 30

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 111

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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

636 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