1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

$_SERVER['HTTP_HOST']; get domain only?

Discussion in 'PHP' started by brianj, Aug 9, 2009.

  1. #1
    Hi, i need help printing the domain of my site like this:
    
    $domain = $_SERVER['HTTP_HOST'];
    PHP:

    anyone can help me to only print "domain.com" ?


    Thanks in advance,
     
    brianj, Aug 9, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    HTTP_HOST will print whatever host you're on, so if you're on www.domain.com it'll say www.domain.com. IF you just want to auto strip the www in front off of it (basically allowing subdomains)

    
    $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
    
    PHP:
    Course I generally use $_SERVER["SERVER_NAME"] bout the same thing though.
     
    kblessinggr, Aug 9, 2009 IP
  3. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    thanks alot;)

    any idea how to strip also subdomains and .com extension? Basically i only wan't to get the domain name out, but without errors..
     
    brianj, Aug 9, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Assuming the domain you're grabbing is not a .co.uk or .com.tw etc and just your basic .com .net .org .ca. .us .me .info, you could do this.

    
    $darray = explode('.', $_SERVER['HTTP_HOST']);
    $narray = array_reverse($darray);
    $domain = $narray[1];
    unset($darray, $narray);
    
    PHP:
    What would happen, lets say your domain is www.domain.com

    explodes it by . thus making $darray an array of [www][domain][com]
    But since subdomains could be multiple words, and we know with the exception of co.uk and similar TLDs that the domain is second to last we can quickly reverse the array to get [com][domain][www], then we pull the value of the second element (arrays start at zero) instead of having to count the elements then subtract one.
     
    kblessinggr, Aug 9, 2009 IP
  5. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    wohah, thanks!! Such a great explanation, i start getting used to PHP.. but it's still a nightmare to me^^
     
    brianj, Aug 9, 2009 IP