Domain availability function PHP

Discussion in 'PHP' started by elsjaakie, Mar 15, 2009.

  1. #1
    Hello guys,

    I have a small question about a domain availability function.. I'm inneed of an accurate function that returns a "1" if the checked domain is registered. At the moment im using this :

    
    function iswebexist($url)
    {
    $url = strtolower($url);
    $d = gethostbyname($url);
    if ($d!=$url) {return(1);}
    }
    ?>
    
    Code (markup):
    It works, but the problem is its only checking if the hostname is alive, not if its registered. Does anyone have ideas how to modify this function so that i can see results from domains that really are registered?

    Thanks in advance.
     
    elsjaakie, Mar 15, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to use whois to see if a domain is registered. And you should cache the results because most (all?) whois servers rate-limit searches. Anyway, google for "php whois" and I'm sure you'll find lots of goodies.
     
    SmallPotatoes, Mar 15, 2009 IP
  3. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #3
    Yep, check for a nice whois class on phpclasses.org
     
    crivion, Mar 15, 2009 IP
  4. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you have domain registrar account, check if your provider has API access (Enom does), then look for php class that implements this API. For example, this is how I use Enom API class available at phpclasses.org:

    
    require_once('class.EnomService.php');
    
    $enom = new EnomService('username', '********', false, true);
    
    $result = $enom->checkDomain('domain_name', 'com');
    
    PHP:
     
    steelaz, Mar 15, 2009 IP
  5. elsjaakie

    elsjaakie Peon

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks guys.. Im really not a programmer so i cant script this stuff on my own. So i need a little help :p

    I found this on some website:
    (http://www.bin-co.com/php/scripts/online/check_domain_availability/)

    
    function checkDomainAvailability($domain) {
        if(preg_match('/[;\&\|\>\<]/', $domain)) exit; //Could be a hack attempt
        
         exec("whois " . escapeshellarg($domain), $output); //:CAREFUL:
         $result = implode("\n", $output);
        
        return (strpos($result, 'No match for domain') !== false);
    }
    
    Code (markup):
    Can someone rewrite this function so it returns a "1" if a domain is registered?

    Thanks!
     
    elsjaakie, Mar 15, 2009 IP
  6. REALLY Made IN Canada

    REALLY Made IN Canada Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The returned results would have to be parsed and because of the varying nature of formats by different registrars this may be the "scenic route" to your solution.

    The API approaches mentioned above are more practical.

     
    REALLY Made IN Canada, Mar 15, 2009 IP
  7. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    
    
    function checkDomainAvailability($domain) {
        if(preg_match('/[;\&\|\>\<]/', $domain)) exit; //Could be a hack attempt
        
         exec("whois " . escapeshellarg($domain), $output); //:CAREFUL:
         $result = implode("\n", $output);
        
       if (strpos($result, 'No match for domain') !== false){
          return(1);
       } else {
          return(0);
       }
    }
    
    
    PHP:
    I didn't tested it, but it should work :)

    I hope this will help you.
     
    Ilyes, Mar 15, 2009 IP
  8. elsjaakie

    elsjaakie Peon

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    the script works thanks for it, but this one gives very strange results hehe. So im going to try to learn more about the API solution. Its a better option i guess..
     
    elsjaakie, Mar 15, 2009 IP
  9. elsjaakie

    elsjaakie Peon

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I found the solution i think

    
    $result = dns_get_record($url);
    if (!empty($result)) { return(1);}
    
    Code (markup):
    Easy and works better then my first attempt :p

    Thanks for your help guys!
     
    elsjaakie, Mar 15, 2009 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It's kind of a 98% solution; domains that are registered but have no RRs are rare, but they do exist. To be completely accurate you need to use whois. But it may not be worth the trade-off, depends on your particular needs.

    Also your approach will fail to catch reserved and otherwise unavailable names. For example, there are no RRs for "r.com" but nevertheless is not open for registration. Whois would tell you this but DNS will not.
     
    SmallPotatoes, Mar 15, 2009 IP
  11. bizhobby

    bizhobby Peon

    Messages:
    692
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #11
    dns_get_record is what I recommend for quick domain check as well.

    I want to make a note however that in order to resolve a domain, it needs to have a nameserver record.
    So if you use DSN_NS as a second parameter it would be a lot quicker and efficient.
    (It is highly unlikely for registered domains to not have any name servers, but as someone pointed out they do exist).

    
    $result = dns_get_record($url, [B][COLOR="Blue"]DSN_NS[/COLOR][/B]);
    if (!empty($result)) { return(1);}
    
    Code (markup):
     
    bizhobby, Oct 8, 2009 IP