Here is how to quickly check if domain is available...

Discussion in 'PHP' started by bizhobby, Oct 8, 2009.

  1. #1
    I have seen the wildest answers to this question and would really hate to see some struggle with it.

    Many solutions use a whois lookup which is very slow and in some cases could get your IP address temporarily banned.

    My solution goes right at the source - for a domain to be registered it needs to have a NS (nameserver) record in the DNS so that it can be resolved to an IP address.

    Here is an example that uses the is_domain_available function and finds the available domains from sports2000.com .. sports2099.com.

    <pre>
    <?php
    
    // This is the key one
    [B]function is_domain_available($domain)
    {
      // quickly check for NS record
      return (count(dns_get_record($domain, DNS_NS)) == 0);
    }[/B]
    
    list($usec, $sec) = explode(' ', microtime());
    $script_start = (float) $sec + (float) $usec;
    
    for($i=0; $i<100; $i++)
    {
      $domain = sprintf("sports2%03d.com", $i);
      if (is_domain_available($domain))
      {
        print "$domain is free!\n";
      }
    }
    
    list($usec, $sec) = explode(' ', microtime());
    $script_end = (float) $sec + (float) $usec;
    
    $elapsed_time = round($script_end - $script_start, 5);
    
    print "Elapsed time: $elapsed_time\n";
    ?>
    </pre>
    
    Code (markup):
    This function checked all 100 domains in 9 seconds.
    I am open to suggestions/ideas on how to improve speed.

    Has anyone tried connecting directly to OpenDNS servers?
     
    bizhobby, Oct 8, 2009 IP
    elusid likes this.
  2. silotka

    silotka Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ..

    ++$i is faster than $i++
    echo is faster than print
    $domain = sprintf("sports2%03d.com", $i); is useless and slow..
     
    silotka, Oct 8, 2009 IP
  3. bizhobby

    bizhobby Peon

    Messages:
    692
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #3
    silotka, thank you for the feedback!

    The question I am really concerned about is how to speed up is_domain_available.

    What is a good alternative for sprintf?
     
    bizhobby, Oct 8, 2009 IP
  4. elusid

    elusid Peon

    Messages:
    890
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the handy hack I will stash that in my library :)
     
    elusid, Oct 8, 2009 IP