Checking if the site resolve a DNS or not I've written code to check whether a domain resolves DNS or not like follows. It is working fine, but I doubt whether there is a simple way to check this by using `dns_get_record'

$url = 'http://www.example.com';
$get = get_headers($url); //Fetches all the headers sent by the server in response to a HTTP request
if (preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/', $get[0])){
$parse = parse_url($url); //Parse a URL and return its components
$ip = gethostbyname($parse['host']);  // Get the IPv4 address corresponding to a given Internet host name 
//ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address
return (ip2long($ip) == true) ? true : false;


Any suggestions would be much appreciated.
You are doing way more than necessary, if all you want to verify is that the hostname resolves to an IP address.

gethostbyname() comes pretty close to doing the job:


  Returns the IPv4 address or a string containing the unmodified hostname on failure.


Actually, that's annoying.  Try gethostbynamel() instead:


  Returns an array of IPv4 addresses or FALSE if hostname could not be resolved.


So, you could just do

$url_parts = parse_url($url);
return gethostbynamel($url_parts['host']) ? true : false;




Note, however, that the gethostbyname() family of functions relies on the operating system to do the name resolution in whatever way it is configured.  Usually, that means using DNS.  However, it might also use a local hosts file, NIS, LDAP, Multicast DNS, etc.  dns_get_record() would actually perform a DNS lookup, independent of the machine's preferred name resolution mechanism.  If you want to go that route, then it looks like checkdnsrr($host, 'A') would be convenient.

The distinction is almost always inconsequential, but I thought I'd mention it since you specifically mentioned DNS resolution in your question.