Zend - The PHP Company




Miscellaneous

Add Code


A Better Full, Independent Whois.  

Type: class
Added by: mallan2
Entered: 13/07/2001
Last modified: 07/12/2000
Rating: - (fewer than 3 votes)
Views: 8004
Works by fetching the whois server from whois.crsnic.net if com,net,org,edu address, sets the whois server it to countrycode.whois-servers.net for au,uk,etc. and sets the whois server to whois.arin.net for ips. Then it queries the aproprite whois server.


<?

/*
Copyright (c) 2001, Martyn Allan
All rights reserved.

Usage:
    // To setup:
    $whois = new whois;
    $whois->setDomain($domain); // $domain is the domain you wish to lookup.
    $whois->find;
    $whois->lookup;
    $whois->ip_lookup; // If you are looking up an ip.

    // To view:
    echo $whois->server; // The whois server used to get the info.
    echo $whois->header; // The Information recieved from "whois.crsnic.net" (for com,net,org,edu Domains and IP's only).
    echo $whois->nomatch; // Did not find a match in "whois.crsnic.net".
    echo $whois->domain; // The domain.
    echo $whois->ip; // The ip (If you are looking up an ip).
    echo $whois->body; // The info on the domain.
    echo $whois->ipbody; // The info on the ip.
*/

function OutStr ($string_haystack$needle_start$needle_finish$pos_add=0) {
  if (
strpos  ($string_haystack$needle_start)) {
    
$start strpos  ($string_haystack$needle_start)+$pos_add;
    
$count strpos ($string_haystack$needle_finish$start)-$start;
    return 
substr ($string_haystack$start$count);
  }
}

class 
whois {
  var 
$server$header$body$ipbody$domain$ip$step$nomatch;

  function 
setDomain ($lookup) {
    
$this->domain $lookup;
    
$this->step 1;
  }

  function 
find () {
    if (
$this->step >= 1) {
      
$lookup $this->domain;
      
$tld array_reverse (explode ('.'$lookup));
      
$this->nomatch false;
      if (
preg_match ("/d{1,3}/"$tld[0])) {
        
$this->ip $lookup;
      }
      if (
preg_match ("/com|net|org|edu|d{1,3}/i"$tld[0])):
        
$whois "whois.crsnic.net"// rs.internic.net is an alternative address.
        
$fp fsockopen ($whois43, &$errno, &$errstr30);
        if (!
$fp) {
          
printf ("Error: %s (%s)"$errstr$errno);
          
$this->header 0;
          return;
        }
        
$lookup .= "n";
        
fputs ($fp$lookup);
        
$this->header fread ($fp16384);
        
fclose ($fp);
        if (
strpos ($this->header"No match for")) {
          
$this->nomatch true;
          return;
        }
        
$look_a strtolower (OutStr ($this->header"Server Name:""n"13));
        
$look_b strtolower (OutStr ($this->header"Domain Name:""n"13));
        if (
$look_a) {
          
$look_p array_reverse (explode ('.'$look_a));
          
$this->domain $look_p[1].".".$look_p[0];
        }
        if (
$look_b) {
          
$this->domain $look_b;
        }
        
$this->server strtolower (OutStr ($this->header"Whois Server:""n"14));
      else:
        
$this->header '';
        
$this->server strtolower ($tld[0]).".whois-servers.net";
      endif;
      
$this->step 2;
    }
  }

  function 
lookup () {
    if (
$this->step >= 2) {
      
$lookup $this->domain;
      
$whois $this->server;
      
$fp fsockopen ($whois43, &$errno, &$errstr30);
      if (!
$fp) {
        
printf ("Error: %s (%s)"$errstr$errno);
        
$this->body 0;
        return;
      }
      
$lookup .= "n";
      
fputs ($fp$lookup);
      
$this->body .= fread ($fp16384);
      
fclose ($fp);
      
$this->step 3;
    }
  }

  function 
ip_lookup () {
    if (
$this->ip) {
      
$lookup $this->ip;
      
$whois "whois.arin.net";
      
$fp fsockopen ($whois43, &$errno, &$errstr30);
      if (!
$fp) {
        
printf ("Error: %s (%s)"$errstr$errno);
        
$this->ipbody 0;
        return;
      }
      
$lookup .= "n";
      
fputs ($fp$lookup);
      
$this->ipbody .= fread ($fp16384);
      
fclose ($fp);
      
$this->step 4;
    }
  }

}

?>


Usage Example


<? require("whois.inc"); ?>
<form method="POST" action="<? echo $PHP_SELF?>">
<input type="text" name="lookup">
<input type="submit" value="lookup">
</form>
<?
  
if($REQUEST_METHOD == "POST"){
    
$whois = new whois;
    
$whois->setDomain($lookup);
    
$whois->find();
    if (!
$whois->nomatch$whois->lookup();
    if (
$whois->ip$whois->ip_lookup();
    
printf("<pre>%sn%sn%s</pre>n"$whois->header$whois->body$whois->ipbody);
  }
?>


Rate This Script





Search



This Category All Categories