Solved

$_SERVER["SERVER_NAME"] to get domain only

Posted on 2012-04-03
8
327 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
Comment Utility
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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
Comment Utility
What about using $_SERVER['SERVER_ADDR'] or localhost or 127.0.0.1 ? In conjunction with $_SERVER['SERVER_PORT']?

Bye, Olaf.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
// 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now