How to determine host name?

Discussion in 'PHP' started by tdd1984, Feb 11, 2012.

  1. #1
    I a just curious how do I determine the host name. For example when I look into my Piwik traffic logs. It says Comcast, ATT, SBCGlobal, and etc. How do I return it in that format. I tried using $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); , but it echoed the full host name out.
     
    tdd1984, Feb 11, 2012 IP
  2. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #2
    You could trim the sub domain out of remote address, and render a result, but this isn't that effective as most ISP's may use another domain for their business presence. For example my ISP is bigpond.com yet for me my host address is something like ZZZ-xxx-xxx-x-xxx.ZZZZx.zzz.bigpond.net.au. The only way I think you can extract ISP business names like the ones you mention is to make a remote whois call for gethostbyaddr() and if possible via a whois API or proxy to remote-host whois query.

    Below is a bot I cooked up that should do the job, though I strongly recommend that you add a set of whois sites passing them with a boolean argument (if first queried site returned empty) just incase remote whois site blocks your domains IP.
    you will need to seek the string positions start & end coordinate tags in strpos for each whois site.
    Avoid querying whois. domaintools. com as that site blocks anyone after a few queries anyway.


    <?php
    /* ::License::  Buy-me-a-Beer :cool: [paypal: nettsight(about)gmail dot com] //&Public Domain: by jay @http://he.tel */
    
    
    $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
    
    echo "<b>Host/ISP:</b> ", $hostname, "<br>", "\n";
    
    /* Recommended: try to utilize socket proxy VPN or similar cloaking method to protect your query from blockage! */
    $locate_hostname = "http://who.is/whois/". $hostname; ##construct query URI. 
    
    echo "<b>Query String:</b> ", $locate_hostname, "<br>", "\n";
    
    ##(below) disguise cURL - create fake headers for bot##
      function disguise_curl($locate_hostname) {
      $curl = curl_init();
      $header[0] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,";
      $header[] = "Cache-Control: max-age=0";
      $header[] = "Connection: keep-alive";
      $header[] = "Keep-Alive: 0";
      $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
      $header[] = "Accept-Language: en-us,en;q=0.5";
      $header[] = "Pragma: ";
      
      curl_setopt($curl, CURLOPT_URL, $locate_hostname);
      curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Ant.com Toolbar 2.0.1 Firefox/3.6.13');
      curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
      curl_setopt($curl, CURLOPT_REFERER, "http://who.is"); ##make sure we pass genuine referrer ie: the BASE URL! 
      curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
      curl_setopt($curl, CURLOPT_AUTOREFERER, true);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_TIMEOUT, 0);
      curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
      curl_setopt($curl, CURLOPT_TRANSFERTEXT, TRUE);
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); ##follow redirects is essential for sub-level URI's, eg: [url]http://who.is/whois/ZZZ-xxx-xxx-x-xxx.ZZZZx.xxx.bigpond.net.au[/url] redirects to: [url]http://who.is/whois/bigpond.net.au[/url] for example.
      curl_setopt($curl, CURLOPT_MAXREDIRS, 1);         ##number of redirects to follow.
      
      $queried_page = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
      echo "<b>Remote webpage I queried:</b> ", $queried_page, "<br>", "\n";
    
    $html = curl_exec($curl);
    curl_close($curl);
    return $html; 
    }
    
    $content = disguise_curl($locate_hostname);
    $start = strpos($content,'Registrant:')+11;       #check on scraped page source-code for string positions start coordinate tags.
    $end = strpos($content,'Registrant ID:',$start);  #check on scraped page source-code for string positions end coordinate tags.
    $outputA = substr($content,$start,$end-$start);
    $outputB = preg_replace("/&nbsp;/", " ", $outputA);
    $result = preg_replace("/\s\s++/", " ", $outputB);
    
    echo "<b>Your Hostname/ISP provider is:</b> ", "<u>", trim($result), "</u>", "\n";
    
    ?>
    PHP:

    example usage: link-directory.org/../determine host name

    NOTE* strpos will fail if coordinate tags are not found, will return whole page instead.
    You'll need to create a conditional statement with strpos if returns false.

    ROOFIS :cool:
     
    Last edited: Feb 12, 2012
    ROOFIS, Feb 12, 2012 IP