How To Quickly Check Domain Availability

Have you ever wondered how those domain suggestion sites can check so many domain names so quickly? This is how.

Below I’ve listed three techniques for checking domain availability. They’re ordered (loosely) from fastest/cheapest/least accurate to slowest/priciest/most accurate. Ideally, you would run each domain through all three in order, and only assume it’s available if it passes all checks.

1. Check the nightly zone file

The zone file is basically a huge text file that lists all registered domains. Each top-level domain (.com, .net, etc) has its own file. You can load it into a local database and use it as a very quick way to check whether a domain name is available. This will filter out most unavailable names. Note that most zone files are only updated once per day, so you will get some false positives.

You can get the zone file for a particular TLD from the registrar company that controls that zone. Access is usually free, but most registrars will require you to print out and sign an agreement before they’ll let you download the files. Approval can take from several days to several weeks.

If the TLD you’re interested in isn’t listed above, find out which registrar controls the zone and take a look at their homepage. Most will have a some way of getting the zone file.

Alternatively, PremiumDrops.com (not an affiliate link) offers instant access to the nightly zone files – for a moderate fee.

2. Do a DNS lookup

Since you’re reading this post, you probably already know what DNS is. If not: it’s basically the thing that tells your computer what server to connect to when you try to open a webpage or send an email. All domains in active use will have one or more DNS records, so you can check if a domain is available by running a DNS query. This is slower than scanning the zone file – network latency comes into play – but has a better chance of catching domains that have been registered just a few hours ago.

Each OS and programming language has its own way of running DNS lookups. As an example, here are three different ways to do it in PHP:

  • dns_get_record – Most reliable. Works with IPv6. Requires PHP 5+. Should work for domains that have name servers but no IP address (not tested).
     function domainAvailable($domain){
    	$results = @dns_get_record($domain, DNS_ANY);
    	return empty($results);
    }
  • gethostbyname – Reliable, but only for domains that have a valid IPv4 address.
     function domainAvailable($domain){
    	return gethostbyname($domain) == $domain;
    }
    
  • checkdnsrr – Technically this should be easier to use than dns_get_record and at least as reliable. In practice, it doesn’t seem to work with IPv6, and other users have reported issues when using it with the “ANY” record type. Included only for completeness.
     function domainAvailable($domain){
    	return !checkdnsrr($domain, 'ANY');
    }
    

3. Do a WHOIS lookup

The most reliable way to check domain availability is to run a WHOIS query. I’ve left it for last because it’s also the most expensive. Many registrars will let you run a couple of queries for free, but if you want to do bulk lookups, you’ll generally need to part with some cash. Well, that, or have a sizeable botnet at your disposal.

One popular WHOIS service is WhoisXmlApi.com (not an affiliate link). They offer a simple HTTP-based API that returns WHOIS data in XML or JSON format. Depending on the plan, prices range from 1.5 to 0.1 cents per query.  You can also sign up for a free “developer account” that lets you try out the API without paying anything.

Here’s an example of how to use the WhoisXmlApi.com API to check domain availability with PHP:

function domainAvailable($domain, $username, $password){
	//Build the API request.
	$api = 'http://www.whoisxmlapi.com/whoisserver/WhoisService';
	$params = array(
		'domainName' => $domain,
		'username' => $username,
		'password' => $password,
		'outputFormat' => 'json', //Optional. If omitted, the response will be in XML format.
	); 
	$request_url = $api.'?'.http_build_query($params, null, '&'); 
	
	//Send the WHOIS query. I've used file_get_contents() here because it's the simplest, 
	//but you could also use any other way of retrieving a webpage (e.g. CURL) instead.
	$response = file_get_contents($request_url);	
	
	//Decode the response.
	$response = json_decode($response); //PHP 5.2+
	
	//On success, the response object will include a domainAvailability field 
	//containing one of two possible values - "AVAILABLE" or "UNAVAILABLE".
	if ( is_object($response) && isset($response->WhoisRecord->domainAvailability) ){
		return $response->WhoisRecord->domainAvailability == 'AVAILABLE';
	} else {
		return false; //API error. The domain name might be invalid.
	}
}

And that is it. If a domain name passes all three checks, you can be 99% sure it’s available.

Related posts :

5 Responses to “How To Quickly Check Domain Availability”

  1. kannan says:

    Thanks for the post. Quite useful. Would be nice if you had a twitter plugin to tweet your posts.

  2. James Simmons says:

    Hey dude,

    Love the blog. You post about a lot of obscure topics that just happen to all be in my area of interest. Detecting WordPress versions, detecting parked domains, extracting content from pages, etc. are all awesome posts. I hope to see more along those lines!

  3. White Shadow says:

    Thanks! If you have any specific ideas for posts you’d like to see, feel free to send them along.

  4. James Simmons says:

    Well, I’d like to see anything along the lines of web crawling (even high-level like the post about ‘can I crawl the entire internet?’), content extraction, information extraction, detecting various versions of CMSs across the web. Any neat things you come up with in Python are always interesting to check out. Check out my submissions on Hacker News if you want an exhaustive list of the type of content I like to see. :)

    http://news.ycombinator.com/submitted?id=coderdude

  5. Mukesh says:

    searching for domain availablity checker api..
    UmMm
    Hoping it is free available

Leave a Reply