Articles

PHP WHOIS and IPWHOIS functions

Author: nathacof
Published: Wednesday 1st of July 2009

Many webhosts provide WHOIS lookups on their site to check a domain's availability. There's no secret handshake to use this technology; anyone with the proper knowledge can implement WHOIS lookups.

You may be wondering how I perform Whois, and IPWhois lookups on this site. The process is rather simple, and can be demonstrated with two functions.

Here are two PHP functions that will allow you to perform WHOIS and IPWHOIS lookups on your own site!

WHOIS

The logic of this function is borrowed from the Linux whois utility.

<?php 
/*
 *  Function accepts the domain for 
 *  which we want the WHOIS information
 *  default value is example.com
 */
function getWhoisText($domain = 'example.com') {
/*
 *  whois-servers.net provides 
 *  information pertaining to relevant
 *  whois servers
 */
$WHOIS = 'whois-servers.net';

/* 
 * This code breaks a domain into it's 
 * atomic parts and reverses their order
 */
$tmp = array_reverse(explode('.', $domain));

/*
 * Here we set the the appropriate WHOIS
 * server based on the Top Level Domain
 */
$server = $tmp[0] . "." . $WHOIS;

/*
 * Connect to the WHOIS server
 */
if(!$link = fsockopen($server, 43, $errno, $errstr, 15)) {
        trigger_error("Error (" . $errno . "): " . $errstr .
                      "Could not connect to " . $server,  
                      E_USER_NOTICE);
        return false;
}

/*
 * write the domain to the socket
 */
if(!fwrite($link, $domain. "\r\n")){
        trigger_error("Error: Could not write to socket",  
                      E_USER_NOTICE);
        return false;
}

/*
 * collect the returned data
 */
while (!feof($link)){
        $string .= fgets($link, 4096);
}

/*
 * close the socket
 */
fclose($link);

/*
 * return whois information
 */
return $string;
}

IPWHOIS

/*
 *  Function accepts the IP for 
 *  which we want the WHOIS information
 */
function getIpWhoisText($ip, $server = 'ARIN') {
/*
 * Validate IP Address
 */
if(!ip2long($ip)) {
    trigger_error("Invalid IP Address",
                  E_USER_NOTICE);
    return false;
}
/* 
 * List of servers
 */
$SERVERS = array('ARIN'    => 'whois.arin.net',
                 'RIPE'    => 'whois.ripe.net',
                 'APNIC'   => 'whois.apnic.net',
                 'LACNIC'  => 'whois.lacnic.net',
                 'AFRINIC' => 'whois.afrinic.net' );
/*
 * Check that the server exists in our array
 */
if(!array_key_exists($server, $SERVERS)) {
    trigger_error("Invalid Server type, must be one of, " .
                  implode(", ", array_keys($SERVERS)) . ".",
                  E_USER_NOTICE);
    return false;   
}


/*
 * Here we set the the appropriate WHOIS
 * server
 */
$server = $SERVERS[$server];

/*
 * Connect to the WHOIS server
 */
if(!$link = fsockopen($server, 43, $errno, $errstr, 15)) {
        trigger_error("Error (" . $errno . "): " . $errstr .
                      "Could not connect to " . $server,  
                      E_USER_NOTICE);
        return false;
}

/*
 * write the ip to the socket
 */
if(!fwrite($link, $ip. "\r\n")){
        trigger_error("Error: Could not write to socket",  
                      E_USER_NOTICE);
        return false;
}

/*
 * collect the returned data
 */
while (!feof($link)){
        $string .= fgets($link, 4096);
}

/*
 * close the socket
 */
fclose($link);

/*
 * return whois information
 */
return $string;
}

Article Search

Social Networks