3

I have been coding a domain checker and really stuck with the php. This is what i have so far:

<?php
set_time_limit(0);
ob_start();
$domain = $_GET['domain'];
########### Extensions to be checked
$extensions = array(
    '.com'      => array('whois.crsnic.net','No match for'),
    '.info'     => array('whois.afilias.net','NOT FOUND'),  
    '.net'      => array('whois.crsnic.net','No match for'),
    '.co.uk'    => array('whois.nic.uk','No match'),        
    '.nl'       => array('whois.domain-registry.nl','not a registered domain'),
    '.ca'       => array('whois.cira.ca', 'AVAIL'),
    '.name'     => array('whois.nic.name','No match'),
    '.ws'       => array('whois.website.ws','No Match'),
    '.be'       => array('whois.ripe.net','No entries'),
    '.org'      => array('whois.pir.org','NOT FOUND'),
    '.biz'      => array('whois.biz','Not found'),
    '.tv'       => array('whois.nic.tv', 'No match for'),
);
###########

if(isset($domain))
{
    $newdomain = str_replace(array('www.', 'http://'), NULL, $domain);
    $finaldomain = str_replace($extensions, NULL, $newdomain);

    if(strlen($finaldomain) > 0)
    {
        foreach($extensions as $extension => $who)
        {
            $buffer = NULL;

            $sock = fsockopen($who[0], 43) or die('Error Connecting To Server:' . $server);
            fputs($sock, $finaldomain.$extension . "\r\n");

                while( !feof($sock) )
                {
                    $buffer .= fgets($sock,128);
                }

            fclose($sock);

            if(eregi($who[1], $buffer))
            {
                echo '<h4 class="available"><span>Available</span>' . $finaldomain. '<b>' . $extension .'</b> is Available</h4>';
            }
            else
            {
                echo '<h4 class="taken"><span>Taken</span>' . $finaldomain . '<b>' .$extension .'</b> is Taken</h4>';
            }
            echo '<br />';  

            ob_flush();
            flush();
            sleep(0.3);

        }
    }
    else
    {
        echo 'Please enter the domain name';
    }
}
?>

The problem I am having is that i don't know how i could remove the extension from the passed domain.

Then when it returns the results i want the extension they typed to be the first in the results list.

I am new to php but need this for my project. All help appreciated.

Thanks Joe

Makoto
  • 104,088
  • 27
  • 192
  • 230
Joe2010glas
  • 75
  • 1
  • 6

2 Answers2

1

First of all, the extension is called a top-level domain (abbreviated TLD). Secondly, .co.uk is not a top-level domain, .uk is. It also has other subdomains like .org.uk, .gov.uk and so on.

Now, to return the extension part of a filename/domain name, you can use pathinfo:

$tld = pathinfo('helloworld.co.uk', PATHINFO_EXTENSION);
echo $tld;   // uk

You may have to modify your array to remove the headings dots you have put there, or simply:

$tld = '.' . pathinfo('helloworld.co.uk', PATHINFO_EXTENSION);
$whois_server = $extensions[$tld];
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • Hi there thanks for your reply. I was aware about tld's and that but for coding this thought it was best to write it like that :). Im a bit confused with what you mean. As the pathinfo() is just going to store the .uk bit in the $tld variable. Would i do a str_replace() with $tld to remove this from the $domain. Now if it only removes the .uk bit the way i've st the code up it will look for example.co.com, example.co.co.uk etc... Plus once i have it removed how would I arrange results so that (tld) was first. plus where did you get $whois_server, Thanks again netcoder for your great answer. :) – Joe2010glas Feb 16 '11 at 22:04
  • @Joe2010: Well the way you are doing right now is wrong. By obtaining the TLD (i.e.: in `$tld`) you can figure directly what server to query, without the `foreach`. `$whois_server` is a variable I created, the equivalent of `$who` in your code. As for the `str_replace`, you don't need it. Just use `pathinfo` to get the TLD, match it to a server, query it and return whather you want returned. – netcoder Feb 16 '11 at 22:49
  • **Note:** You should not use `ereg`, it's deprecated. Use `preg_match` instead. However, in your code, a simple `strpos` would do. – netcoder Feb 16 '11 at 22:50
1

Instead of having all those Whois servers for each TLD you can just query TLD.whois-servers.net.

From Wikipedia:

whois-servers.net provides DNS alias records (CNAME) for TLD WHOIS servers of the form .whois-servers.net. The GNU WHOIS utility automatically uses the whois-servers.net service.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500