Go Premium for a chance to win a PS4. Enter to Win


PHP Domain availability check

Posted on 2004-03-23
Medium Priority
Last Modified: 2012-08-14
Hi all,

I'm tinkering with a script to let people see if a domain is available to register or not.

The best way I've found so far is to use a whois script and then see if the response includes the words "Not found". I've had success with the one from here (near bottom):


Except I have the problem that it matches too generally, if I search for "microsoft.com" I get lots of jokes like:


So, I'm guessing if someone searched for example.com and someone else was using example.com.another-domain.com it's going to return that and my script, not finding a "Not found" message will claim the domain is taken even if example.com is in fact free.

Any suggestions? Is there a way I can stop matching these sub domains or does someone have a suggestion for a better script altogether? I mean, I don't need whois info, only a Yes or No when it comes to is a domain free or not.
Question by:KKPeters
  • 2

Expert Comment

ID: 10662009
Go to http://www.enom.com/resellers/default.asp?step=1#step1 and sign up for a free resellers account.  You can then use your login/pass to check domain name availability using this script I made:


// domain check via CURL:

$sld = $_POST['sld'];
$tld = $_POST['tld'];
$url = "http://reseller.enom.com/interface.asp?uid=YOURUIDHERE&pw=YOURPASSHERE&command=check&sld=$sld&tld=$tld&responsetype=text";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$serveroutput = curl_exec($ch);
$serveroutput = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $serveroutput);

// close curl resource, and free up system resources
echo "$extpage\n\n\n";

// watch the magic happen
$myarray = array();
$ex = explode("\n",$serveroutput);
foreach ($ex as $value) {
      if ($pos = strpos($value,"=")) {
            $mykey = substr($value, 0, $pos);
            $myvalue = substr($value, $pos+1);
            $myarray = $myarray + array($mykey => $myvalue);

echo "<pre>";
print_r ($myarray);


It will return something like:
    [RRPCode] => 211
    [RRPText] => Domain name not available
And you can just check if ($RRPCode==211), or if ($RRPText == "Domain name not available").

This script requires the CURL package to be installed and compiled with PHP.

You can also use sockets, but I recommend the CURL method.

Author Comment

ID: 10665566
That's great thanks, but if possible I'd prefer not to rely on someone elses service. Just incase they pull the plug or change the way it works, etc.

I'll use it there's no other alternative, but it just seem like I can't be the only person to have wanted to try this!

Accepted Solution

BrianPap22 earned 300 total points
ID: 10667032
Well, you're GOING to rely on someone else's service no matter what. No two ways around that. Right now, you are using the services of one or more whois servers, which, as you note, could change the way it works or pull the plug (although it would kinda create havok by shutting down the whois servers, hehe).

Even when your script is working properly, even besides the false-positive problem you mentioned above, the data in the whois listings aren't guaranteed. It could show an expired (and available) domain as having a whois record, or vice versa. At least with the enom method, you will be getting accurate real-time domain name availability information.  Although I agree there exists the possibility that they change their policy such that you can't access their API with the free reseller account.

All in all, I'm willing to bet either method will work well enough for you.  The whois lookup method will work 99.9% of the time, so unless the reason for your script is to send them off to a domain registration site or something, you should be ok with it.  If indeed you know they are using your form for the purpose of finding domain names to register (and your affiliate registrar doesn't provide you with any API) then I say ride it out with enom for as long as it lasts, and use the whois method as a backup.

You might as well put both methods into the same form. If the enom lookup fails for whatever reason, it reverts to checking via the whois method. Can't lose that way :)

Assisted Solution

griffin36 earned 300 total points
ID: 10673044
If you're willing to still use whois, you can pipe the results it gives through grep (a regular expression parser) to filter out joke domains. For example, to see if microsoft.com is available (it's not of course but I'm just giving an example), use:

whois microsoft.com | grep -i microsoft\.com$

That's a pipe symbol in the middle, a shift-\. The -i switch on grep tells it to ignore case. The backslash in microsoft.com after grep is there to ensure it's looking for a period, otherwise it could match any character. The dollar sign ensures that it only matches lines where microsoft.com appears at the very end of the line. If this returns the name of the domain, it's taken. If it returns nothing, it isn't. Be sure to do the escaping of the period, otherwise it might not work. This can be done with the preg_quote function in PHP. So in PHP, the popen function would look like:

popen("whois $domain | grep -i " . preg_quote($domain) . '$', "r");

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question