Solved

PHP Domain availability check

Posted on 2004-03-23
4
582 Views
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):

http://www.webhostingtalk.com/showthread.php?threadid=231463

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

MICROSOFT.COM.RUNS.ON.AN.8088.ORG
MICROSOFT.COM.RAWKZ.MUH.WERLD.MENTALFLOSS.CA
MICROSOFT.COM.OHMYGODITBURNS.COM

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.
0
Comment
Question by:KKPeters
  • 2
4 Comments
 
LVL 4

Expert Comment

by:BrianPap22
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:

<?php

// 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
curl_close($ch);
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);
      }
}
extract($myarray);

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.
http://www.php.net/manual/en/ref.curl.php

You can also use sockets, but I recommend the CURL method.
http://www.php.net/manual/en/ref.sockets.php
0
 

Author Comment

by:KKPeters
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!
0
 
LVL 4

Accepted Solution

by:
BrianPap22 earned 75 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 :)
0
 
LVL 3

Assisted Solution

by:griffin36
griffin36 earned 75 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");
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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 look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now