29

If I have a domain like www.example.com and I want to check if it is available using DNS records (not whois)...

Is it possible to do this using PHP?

Tim Post
  • 33,371
  • 15
  • 110
  • 174
David19801
  • 11,214
  • 25
  • 84
  • 127
  • 5
    If by "available", you mean available for purchase or registration, you cannot determine this reliably using DNS. For example, `moose9moose2.ph` is available, but has an A record. While `ph.moose9moose2` has no DNS records, but is not available for registration. You can try to add rules to cover all the possible cases, but you'll likely never finish. – David Schwartz Jan 01 '12 at 14:22
  • 1
    @David, Correct, but if dns isn't enough, what would you recommend instead? – David19801 Jan 01 '12 at 14:52
  • 1
    See the question you duplicated: Whois. – KingCrunch Jan 01 '12 at 15:44
  • 1
    It's getting hard to tell what your question is. Is your question "how can I tell if a domain is available in PHP", "how can I tell if a domain is registered in PHP", or "how can I do DNS in PHP"? Why don't you just tell us what you're actually trying to do and ask us how best to do it? Your question is becoming like "I want to help my grandmother buy a house, but I want to do it with a jackhammer. How can I do that best?" – David Schwartz Jan 01 '12 at 15:50
  • 2
    Question is: "how can I tell if a domain is available using PHP (using DNS and not whois)?" That is the question. – David19801 Jan 01 '12 at 15:56
  • 1
    Re-opening, the questions are sufficiently different. – Tim Post Jan 03 '12 at 16:25
  • it seems it is in standard php library, check this function – Mariusz Sakowski Jan 01 '12 at 14:15

6 Answers6

46

You can use checkdnsrr or gethostbyname:

Documentation:

http://www.php.net/checkdnsrr

http://www.php.net/gethostbyname

Example checkdnsrr:

<?php
 if ( checkdnsrr('example.com.', 'ANY') ) {
  echo "DNS Record found";
 }
 else {
  echo "NO DNS Record found";
 }
?>

Example gethostbyname:

<?php
 $domain = 'example.com';
 if ( gethostbyname($domain) != $domain ) {
  echo "DNS Record found";
 }
 else {
  echo "NO DNS Record found";
 }
?>
favo
  • 5,426
  • 9
  • 42
  • 61
  • 1
    This shows as found for everything...example - if ( checkdnsrr('examjhkasdjkhbasdle.com', 'ANY') ) { echo "DNS Record found"; } else { echo "NO DNS Record found"; } – David19801 Jan 01 '12 at 15:43
  • here is the second example using gethostbyname: – favo Jan 01 '12 at 17:39
  • 13
    Solution: for checkdnsrr to work, you must add a period after the domain name, example: checkdnsrr('example.com.',"ANY") – David19801 Jan 01 '12 at 20:30
  • 1
    sorry, i have edited my answer to include the missing dot and also to include my previous comment with the gethostbyname example :) – favo Jan 01 '12 at 23:31
  • 2
    if domain is in pending delete status, neither of these works. better to use a registrar api or parse whois information i think. – alpera Feb 12 '14 at 06:15
  • Is there a way to modify this script to return multiple domains at once? For example: I want to check foo.com, example.com, website.com with one query. – Lanti Nov 10 '15 at 16:46
  • yeah, none of these work really. 1. the domain may not have an SOA record in the registry, 2. it could be on a block list either of these cases would fail any kind of DNS lookup, but the domain is still not available. – Monsters X Apr 27 '16 at 16:58
  • You can use gethostbyname() function also here is demo https://speedysense.com/php-domain-availablity-checker-script/ – Jaykumar Patel Dec 09 '19 at 09:05
2

It depends on what you mean by "available." If you mean available for registration, it is not possible to determine based on DNS information alone. The whois system must be used. An easy way to test is to take an unused domain name and set the nameservers to something invalid. DNS will not be available, but the domain is still unavailable for registration. I just tested the suggestions of checkdnsrr(), gethostbyname(), and dns_get_record(). All show that no DNS is returned for a domain that cannot be registered.

The following question offers some more details: Checking if a domain name is registered

Community
  • 1
  • 1
Matthew Kolb
  • 318
  • 1
  • 8
1

All answers suggest using some PHP library. There's a function in vanilla PHP (version 5 to 7) that fetches DNS records for a specific domain.

Utilising the following PHP line,

sizeof(dns_get_record("example.com"))

you will get an INT corresponding to the number of DNS records. Therefore, consider something alike:

if (sizeof(dns_get_record("example.com")) > 0) echo "Domain has DNS records";
else echo "Domain does not have any DNS records";

Documentation

undefined
  • 1,019
  • 12
  • 24
0

I should go for this package Domain-Availability it's support a lot of Top Level Domains and is written in a Object Oriented way.

0x1ad2
  • 8,014
  • 9
  • 35
  • 48
0

Here's a script to check domain availability for some domains using whois. In this example we check all number domains between 10 and 999 for a given top level domain (spoiler alert: they're mostly taken).

Here are predefined examples for .com .pe .ee .cc and .gg. It runs a whois search for the domain and then looks for the words "no match for domain" in the output.

$tld = "com" ;

$lookFor = array();
$lookFor['com'] = "No match for domain" ; // .com
$lookFor['pe'] = "No Object Found" ; // .pe
$lookFor['ee'] = "Domain not found" ; // ee
$lookFor['cc'] = "No match for" ; // cc
$lookFor['gg'] = "NOT FOUND" ;

$i = 10 ;
while($i < 10000)
{
    $domain = "$i.$tld" ;
    $dig = `whois $domain` ;

    
    if(stripos($dig, $lookFor[$tld]) !== FALSE)
    {
        echo "$domain is available\n" ;
    } else {
        echo "$domain bad\n" ;          
    }
     
     $i ++ ;
     
}
A.Badger
  • 4,279
  • 1
  • 26
  • 18
0

Some of the API that will do the job.

http://www.whoisxmlapi.com/domain-availability.php

http://www.dynadot.com/domain/api.html

http://www.opensrs.com/site/integration/api

Raj
  • 22,346
  • 14
  • 99
  • 142