Jamy Mahabier
Jamy Mahabier

Reputation: 428

Checking if the user has entered a domain name or just the base

I want to create something that does a WHOIS query to see if a domain is available. Now, I've found quite a bunch of ready-made scripts for that, but they all seem to be the same in that you need to specify the base, and then choose which extensions you want and it will do that (so for example, enter 'test', check .com and it will check if test.com is available)

However, I'm trying to do something slightly else. The thing I want it to do is check if the user has entered just the base or the full URL, and then do one or multiple queries accordingly. Example:

If the user enters 'test', I want the script to check for test.com, test.org, test.net, etc. If the user enters 'test.com', I want the script to check JUST for test.com.

I really don't know what to build upon, but Mike Nott's PHP Whois Script looks okay. Of course, suggestions are always welcome.

Now I'm guessing the script flow should go something like this:

  1. User enters query
  2. Script checks for spaces
  3. Script checks if user entered just a base or a full domain name (I'm guessing the way to go about this is to check if there's a dot in there)
  4. If the user entered a full domain name, separate the base and the TLD and store them both in variables and store the 'status' somewhere ($full = true/false)
  5. if ($full = true) {check corresponding whois servers for $base.com $base.net, etc};
  6. if ($full != true) {see what the right server for $tld is and check that one for $base.$tld}
  7. Output results

Of course, if there's a script that does this already, let me know.

Edit: Just so you know, I can do simple if statements and the likes, but steps 3, 4, 5 and 6 in my 'workflow' are the parts I can't figure out.

Edit 2: Thanks for all of your answers, guys! Marcus Adam's theories are indeed valid. My domain reseller (the guys I'm getting my domains from) doesn't offer second-level TLD's like .co.uk though, so that's not a problem. They also don't support IDNs.

From the answers of you guys, I came to the conclusion that the explode function is used to separate the domain name. However, what will explode do when it encounters multiple dots? I'm guessing it'll just add another entry to the array, but that will cause problems. Because if the user then enters (for example) a domain ending in .co.uk, the script will take 'co' as the TLD.

Checking for more than 2 strings in the array is also not an option (I think), because if the user then enters 'sub.domain.com' the script will take 'sub' as the base and 'domain.com' as the TLD.

Also, Marcus Adams, you say that if the whois server shows 'available', that's not a guarantee it's available and I must query the registrar. But how would I go about that? Any ideas?

Thanks guys :)

Upvotes: 0

Views: 1558

Answers (4)

Simone Carletti
Simone Carletti

Reputation: 176402

There is no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list of all top-level domains and the level at which domains can be registered.

This is the main reason why the Public Suffix List exits.

Using the definitions collected in this list you can easily parse a domain name into its parts, extracting the TLD, the domain and the subdomain.

Upvotes: 1

Marcus Adams
Marcus Adams

Reputation: 53830

I've implemented such code, but not in PHP. Here's what I learned.

The algorithm

Be careful because some domains can have first and second level top level domains. For example example.in and example.co.in.

I recommend first trimming the domains of leading and trailing white space and converting them to lowercase.

Since many TLDs have their own whois server, you may as well create a table or array of every possible TLD, in lower case, of course.

Then, you check to see if there is a period. If there is no period, then there's nothing further to do.

If there is a period, then you must extract the TLD. Start at left side and find the first period. Split the string there. If the right side matches one of the TLDs in the list, then everything on the left was the name and everything on the right was the TLD. If there is no TLD match, then move to the next period and repeat until you find a match or you run out of periods.

If you can't find a match, then they've not entered a valid domain. You could try splitting at the last period on the right because maybe they entered www.wiki.example.

If you find a match, you still have to see if there is another period on the left side of the split and trim there or deem it invalid.

Finding the whois server

IANA provides basic information on each TLD, including the whois server.

Root Zone Database

Note that you'll probably be creating regular expressions to evaluate the result from each whois server, as the results are not standardized.

Whois database is not the same as the registrar database

You cannot rely on a domain being available simply because the whois server reports that it's "available". In most cases it will be accurate, but don't make any promises to the user that they can register it. You must query the registrar to see if the domain is available.

The most notable exception is when a domain is about to be deleted. There is a period of up to a day where the whois server will have no record of it, but it has not yet released from the registrar.

Internationalized Domain Names

Do you plan on supporting IDNs?

Upvotes: 1

Carl Zulauf
Carl Zulauf

Reputation: 39558

I'm not sure if a script already exists for this, but your proposed solution will work.

Assuming all of the whois lookup logic is in a method called check_whois($base, $tld) the solution might look something like this:

$pieces = explode(".", $user_input);
$base = $pieces[0];
$tld = $pieces[1];
if ($tld == null) {
  $default_tlds = array("com","net","org");
  foreach( $default_tlds as $tld ) {
    check_whois($base, $tld);
  }
} else {
  check_whois($base, $tld);
}

Upvotes: 0

Drahcir
Drahcir

Reputation: 11972

The best way to check is to loop through all the possible domain extensions, split the string with explode and then check whatever comes after the dot.

This is for step 3 & 4:

function isFullDomain($domain){
    $domain=explode('.', $domain);
    foreach($possibleDomainExts as $ext){
        if($ext==$domain[count($domain)-1) return $ext;
    }
    return false;
}

call it like this:

if(!$ext=isFullDomain($domain)){
    //is only the base
}
else{
    //is the full domain and $ext will hold the value of the domain extension
}

You already have that PHP whois script, so take it apart to get a list of possible domains, and also for help with steps 5 & 6

Upvotes: 0

Related Questions