Post Reply 
Script to Check if Domain Name is Available
03-20-2009, 08:55 PM
Post: #1
Script to Check if Domain Name is Available
This is a cut and paste from one of my scripts that I wrote years ago (so there is likely to be some outdated coding methods here and even the registry's may be invalid) when I was automating domain registration for a hosting service I was offering. So it probably won't work out of the box, but will hopefully give you an idea of how to code this kind of thing Cool

PHP Code:
//check domain availability
if (isset($_POST['domain'])){
$domain trim(strtolower($_POST['domain']));
$domain str_replace("www."""$domain);
$domain str_replace("http://"""$domain);
//validate domain name
$domainValid preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9]$/"$domain);
if (!
$domainValid$domain ''//invalidate it
$ext strtolower($_POST[ext]);
$url=$domain.$ext;
switch(
$ext){
case 
".co.uk":
$whois "whois.nic.uk";
break;
case 
".com":
case 
".net":
$whois "whois.verisign-grs.com";
break;
case 
".org":
$whois "whois.pir.org";
break;
case 
".biz":
$whois "whois.biz";
break;
case 
".info":
$whois "whois.afilias.info";
break;
case 
".name":
$whois "whois.nic.name";
break;
case 
".uk.com":
case 
".gb.com":
case 
".uk.net":
case 
".gb.net":
$whois "whois.nomination.net";
break;
case 
".eu.com":
$whois "whois.centralnic.com";
break;
case 
".cc":
$whois "whois.nic.cc";
break;
default:
$whois "whois.internic.com";
//switch

$avail 0;
if (
$domain <> "") {
  
$f fsockopen($whois43$errno$errstr30);
  if (!
$f) {
     echo 
"Connection To Server Failed ($errno)";
  } else {
     
fputs($f"$url\r\n");
         
$who '';
   while (!
feof($f)) {
        
$s strtolower(fread($f,128));
                
$who .= $s;
                
$pos1 strpos($who'no match');
                
$pos2 strpos($who'not found');
        if (
$pos1 !== false$avail 1;
        if (
$pos2 !== false$avail 1;
     }
//while
     
fclose($f);
  }
//$f
if ($avail == 1){
  
$price 100;//getPrice($ext);
    
$years 2;//getYears($ext);
echo "<br /><p class=\"blue\">Congratulations <b>$url</b> is available!</p>\n<p>The price is &pound;$price for $years year(s).</p>
<form action=\"\" method=\"post\">
<input type=\"hidden\" name=\"url\" value=\"$url\" />
<input type=\"hidden\" name=\"ext\" value=\"$ext\" />
<button type=\"submit\">Add to basket</button>
</form>"
;
        } else {
          echo 
"Sorry, <b>$url</b> is already registered.<br><br>Here is the whois data:<br>\r\n<pre>\r\n$who</pre>\r\n";
        }
//avail
}else{
echo 
"<p class=\"warn\">Invalid domain entered!</p>Please try again.";
}
//($domain <> "")
}//post 

MLM Hosting
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: