Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

580 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