SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What should I use? SERVER_NAME or HTTP_HOST

    I am wondering what I should be using for my situation. Basicaly, I have a script where I need to get the hostname/domain name information in a reliable way.

    PHP Code:
    echo $_SERVER['SERVER_NAME'];
    echo 
    $_SERVER['HTTP_HOST']; 
    Sometimes when I use server_name", it only shows me the IP. But I always thought this is more "reliable" than http_host.

    Any thoughts?

  2. #2
    SitePoint Guru enygmadae's Avatar
    Join Date
    Sep 2002
    Location
    Dallas, Tx.
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    echo "Host: ".gethostbyaddr($_SERVER['REMOTE_ADDR']); 
    ?>
    that might do what you want...
    PHP News, Views and Community: http://www.phpdeveloper.org

  3. #3
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really looking for different methods of getting the hostname..

    What I'm looking for is the most "reliable" method that will work over the maximum number of browsers, servers, php installations, etc..

    Thanks!

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may not be right about this but I am assuming that the difference betweeen SERVER_NAME and HTTP_HOST will be this:

    say we have the apache virtual host:
    Code:
    <VirtualHost *>
       ServerName foo.com
       ServerAlias www.foo.com
    </VirtualHost>
    If a user requests:
    http://foo.com/

    Then SERVER_NAME will be 'foo.com'
    and HTTP_HOST will be 'foo.com'

    However, if the user requests
    http://www.foo.com/

    Then the SERVER_NAME is still 'foo.com'
    but the HTTP_HOST is 'www.foo.com'

    This is my guess. Am I right?

  5. #5
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... I'm asking this because I had a situation where the SERVER_NAME was being returned as an IP address.. and the HOST_NAME was being returned as www.domain.com

    I need the "domain.com", and not the IP address.. but in the past people told me I should use SERVER_NAME to do this.

    So now that I know it doesn't always work, I am asking what is the most reliable method to get the hostname.

    freakysid, from what you describe.. it sounds like I should use HOST_NAME.

    Has anyone had any experience where there was no hostname returned when using $_SERVER['HOST_NAME']? Will this return the domain name 100% of the time? (No matter if it is name-based hosting, IP-based hosting, or whatever..)

    Thanks!

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is 99.9% of the time good enough?

    If the client (browser) does not send a HOST header with their HTTP request, and the virtual server for the domain is name based (as most are these days), the request cannot be processed by the web server and the request will end up in the error-log and an error will be returned to the client (I don't know which number).

    So, yes, I would say that using the HOST_NAME is a safe bet and is the most appropriate for your purpose.

  7. #7
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't add too much because freakysid's explaination was exhaustive and complete ,
    I only I add, but it was already said though, that in a HTTP request the Host field in the Header is obligatory ( it could be empty eventually ).
    That's for version 1.1 of the protocol, though.
    So, It's interesting to show the difference here:

    Http 1.1 request
    Code:
    GET / HTTP/1.1
    Host: www.sitepoint.com
    Old Http 1.0 request
    Code:
    GET / HTTP/1.0
    >> I'm asking this because
    >> I had a situation where the SERVER_NAME
    >> was being returned as an IP address
    SERVER_NAME,
    reflects the value set inside Apache httpd.conf,
    in your case (IP address returned)
    I think your servername was probably set as
    ServerName x.x.x.x

    >> will be returned to the client
    >> (I don't know which number).
    400 (Bad Request),
    the http specification said that.
    Doing some tests I had 503,
    but that's another matter...



    pippo
    Last edited by pippo; Dec 2, 2002 at 00:43.
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Curiosity killed the cat.

    Why do you want to get details of the SERVER? Surely you know where your code is hosted? (I guess you realise that this method will not give you details about the CLIENT).
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Curiosity killed the cat.

    Originally posted by siteguru
    Why do you want to get details of the SERVER? Surely you know where your code is hosted? (I guess you realise that this method will not give you details about the CLIENT).
    I have no idea where the code will be hosted.. this is why I need to somehow find out.

    Thank you everyone who responded. I will use $_SERVER['HOST_NAME'] to get the correct domain name.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •