Quick and Easy PHP WHOIS Script

Jacob Allred
#web-dev

I like to be able to type “whois somedomain.com” in my browser and have it bring up the WHOIS information. The site I’ve been using has a captcha (annoying) and has gotten unreliable, so I wrote this quick and easy script to pull the full unformatted WHOIS data for me:

<?php $d =  isset($_GET['d']) ? $_GET['d'] : ''; ?>
<html>
<head>
<title>WHOIS <?php echo $d; ?></title>
</head>
<body>
<pre>
<?php
if(!empty($d)){
    exec("whois ".escapeshellarg($d), $results);
 
    if(isset($_GET['debug'])){
        print_r($results);
    }
 
    $server = '';
 
    foreach($results as $result){
        if(stripos($result, 'whois server:')){
            $server = trim(str_replace('whois server:','',strtolower($result)));
        }
    }
 
    if(!empty($server)){
        passthru("whois -h ".escapeshellarg($server)." ".escapeshellarg($d));
    }else{
        foreach($results as $result){
            echo $result.PHP_EOL;
        }
    }
 
}
?>
</pre>
</body>
</html>

This script should work out-of-the-box for most Linux installations.

You can make the “whois somedomain.com” functionality work in Firefox by creating a bookmark with a location of: http://www.yourdomain.com/whois.php?d=%s

I’d highly recommend you put this script in a password protected directory to prevent abuse!