?
Solved

$_SERVER["SERVER_NAME"] to get domain only

Posted on 2012-04-03
8
Medium Priority
?
359 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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
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…
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

764 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