<?

// TLD  - WHOIS SERVER                     - FREE RESPONSE
// nl   - whois.domain-registry.nl         - hghghghghgh.nl is free
// com  - whois.internic.net               - No match for "JWSCRJJKJKKKJKJK.COM".
// info - whois.afilias.info               - NOT FOUND
// biz  - whois.neulevel.biz               - Not found: hghghghghgh.biz
// net  - whois.internic.net               - No match for "HGHGHGHGHGH.NET".
// org  - whois.publicinterestregistry.net - NOT FOUND

// An array with mappings for TLD's whois servers and 
// available responses
$mappings = array(
    
'nl' => array(
        
'whois' => 'whois.domain-registry.nl',
        
'avail' => '%domain%.nl is free',
        
'command' => 'is ' // The 'is' command bypasses the day limit
    
),
    
'com' => array(
        
'whois' => 'whois.internic.net',
        
'avail' => 'No match for "%domain%.net".',
        
'command' => ''
    
),
    
'info' => array(
        
'whois' => 'whois.afilias.info',
        
'avail' => 'NOT FOUND',
        
'command' => ''
    
),
    
'biz' => array(
        
'whois' => 'whois.neulevel.biz',
        
'avail' => 'Not found: %domain%.biz',
        
'command' => ''
    
),
    
'net' => array(
        
'whois' => 'whois.internic.net',
        
'avail' => 'No match for "%domain%.net".',
        
'command' => ''
    
),
    
'org' => array(
        
'whois' => 'whois.publicinterestregistry.net',
        
'avail' => 'NOT FOUND',
        
'command' => ''
    
)
);

// The regexp pattern
$pattern "/^[a-z0-9][a-z0-9-]*[a-z0-9]$/";

// The default whois server connection port
$port 43;

// The connection/socket timeout in seconds
$timeout 10;

// Verify the received data
if (!preg_match($pattern$domain strtolower(@$_REQUEST['domain']))) {
    print 
"Missing or invalid domain parameter. Parameter value should match the pattern: <code>$pattern</code>";
} else {
    foreach (
$mappings as $tld => $mapping) {
        list(
$server$available$command) = array_values($mapping);
        
$available str_replace("%domain%"$domain$available);

        if (
$fp = @fsockopen($server$port$errno$errmsg$timeout)) {
            
$domain_free false;
            
fputs($fp"$command$domain.$tld\r\n");
            
socket_set_timeout($fp$timeout);

            while (!
feof($fp)) {
                if (
stristr(fgets($fp4096), $available)) {
                    
$domain_free true;
                    break;
                } 
            }
            
fclose($fp);
            echo 
"$domain.$tld is ", ($domain_free '' 'un'), "available<br />\n";
        } else {
            print 
"Unable to check $domain.$tld<br />\n";
        }
    }
}

?>