Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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 "" I get lots of jokes like:


So, I'm guessing if someone searched for and someone else was using it's going to return that and my script, not finding a "Not found" message will claim the domain is taken even if 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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 10662009
Go to 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 = "$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 is available (it's not of course but I'm just giving an example), use:

whois | 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 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

719 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