3

Here is my current code:

function get_cmd ()
{
    if (file_exists('/usr/local/bin/whois'))
        $cmd = '/usr/local/bin/whois';
    elseif (file_exists('/usr/bin/whois'))
        $cmd = '/usr/bin/whois';
    elseif (file_exists('/bin/whois'))
        $cmd = '/bin/whois';
    else
        die('whois shell command does not exist');

    return $cmd;
}

function get_whois ($cmd, $domain) 
{
    if (checkdnsrr($domain))
        $result = shell_exec(escapeshellcmd($cmd ." ". $domain));
    else
        $result = 'DOMAIN IS NOT REGISTERED';

    return $result;
}

$cmd = get_cmd();
echo get_whois($cmd, 'google.com');

now, is there a different method that will easily allow me to extract the expiration date of the domain without having to come up with a whole bunch of different regex? since the information will be formatted differently for every domain...

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
scarhand
  • 4,269
  • 23
  • 63
  • 92
  • 3
    Most WHOIS responses will contain something formatted as `Expiration Date: 02-jun-2013`. Of course you're right that there are several different formats that pop up. You may want to look at this question: http://stackoverflow.com/questions/36817/who-provides-a-whois-api – Michael Mior Jul 06 '11 at 18:29
  • There are now over 1000 TLDs in existance and that number will only increase, this is why I stopped trying to roll my own solution and went with a hosted one. Instead of writing a parser for each TLD (because most of them differ in format, even if only slightly) I started using jsonwhoisapi.com. On their free tier its 10,000 requests/month so it's still free for my small website.. – sousdev Aug 10 '16 at 21:28
  • No, do not shell out to run a whois command, use the libraries inside your programming language or open a TCP socket on port 43, see RFC3912 – Patrick Mevzek Jan 04 '18 at 15:25

2 Answers2

0

This code will give you the expiration date

<? 
$detail = "whois " . $_GET['domain']; 
$res = shell_exec($detail); 
$start = strpos($res,"Expiration"); 
echo substr($res,$start+16,11); 
?>
Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
  • 1
    -1, input is not sanitized or even escaped; this method only works for a very specific format of whois which probably doesn't account for even 5% of all websites. It will fail even the simplest domains such as yahoo.com and google.com. – Wiz Oct 13 '13 at 17:02
  • No, do not shell out to run a whois command, use the libraries inside your programming language or open a TCP socket on port 43, see RFC3912 – Patrick Mevzek Jan 04 '18 at 15:26
0

i've went ahead and just used regular expressions for this. some registrars don't even provide expiration dates in their whois.

scarhand
  • 4,269
  • 23
  • 63
  • 92