Solved

$_SERVER["SERVER_NAME"] to get domain only

Posted on 2012-04-03
8
343 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
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
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.

 
LVL 109

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 109

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 109

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 …

808 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