Volodymyr Osypov blog

May 15, 2008

Simple WHOIS

Filed under: PHP — Tags: , , — admin @ 3:30 pm

PHP gives us the opportunity to run a shell command, for example with exec() function. Let's try it and write a simple whois service. Whois - is a *nix command which will show us the information about domain registration. Also we can check if the domain is free. Remember to check if your hosting provider turned exec() on. Usually this function is in disable_functions parameter in php.ini file. And also try if whois is allowed and works ok on your server. I tried it locally on Apache 2, PHP 5, Linux Fedora 8 and everything was fine.

So we're going to create page with form to input the domain name.
Of course we need to check if domain name is correct, otherwise someone can make an injection and damage our system using OS commands.
Also we will check if exec() PHP command is turned on.

PHP:
  1. <?
  2. if (isset($_GET['domain'])) // if we have entered not a domain, but URL let's taking away http://www.
  3.     {
  4.     $_GET['domain']=str_replace("http://www.","",$_GET['domain']);
  5.     $_GET['domain']=str_replace("https://www.","",$_GET['domain']);
  6.     }
  7. // form output
  8. echo '<form name="domaincheck" action="whois.php" method="get">';
  9. echo '<input type="text" name="domain" size="50" value="';
  10.  if (isset($_GET['domain']))
  11.     echo $_GET['domain'];
  12. echo '">
  13. <input type="submit" value="Check">
  14. </form><br /><br />';
  15. // is exec() turned on ?
  16. if (!function_exists("exec")) {
  17.     echo "ERROR: exec() is not available!";
  18. }
  19. else
  20. {
  21. if (!isset($_GET['domain'])) {
  22.         echo "ERROR: domain is not specified";
  23.     }
  24.     else {
  25.       if (!eregi("^[a-z0-9.-]*$",$_GET['domain'])) { // simple domain check: only letters,digits and ".", "-"
  26.         echo "ERROR: incorrect domain!";
  27.       }
  28.       else {
  29.         exec("whois ".$_GET['domain'],$output,$return_var); // let's run whois command
  30.         foreach ($output as $line) // let's print each line
  31.             if (!eregi("^\[.*\]$",$line)) // we don't need the lines like "[Redirecting hostname]"
  32.                  echo $line."<br />";
  33.       }
  34.     }
  35. }
  36. ?>

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress