$domain= "stackexchange.com";
if (checkdnsrr($domain,"MX")){
echo "yup! " . $domain ." is valid domain";
}
else{
echo "nope " . $domain ." invalid domain!";
}
I used this code to check whether entered domain is valid or not. Its working But when i give sub-domain in it, Its says domain not found
$domain= "wordpress.stackexchange.com";
if (checkdnsrr($domain,"MX")){
echo "yup! " . $domain ." is valid domain";
}
else{
echo "nope " . $domain ." invalid domain!";
}
If i insert this code, its coming as, domain not found. Does anyone having solution for this??
As the user Linus pointed out, a sub-domain will not necessarily have a MX
record. Better check for the A
record.
Even then, a domain might have valid DNS records, yet the site might not be alive.
A better way to check if the site is alive is using fsockopen() function.
For example:
function isValidSite($url, $port = 80, $timeout = 1) {
$isValid = false;
$check = @fsockopen($url, $port, $errno, $errstr, $timeout);
if ($check) {
$isValid = true;
@fclose($check);
}
return $isValid;
}
var_dump(isValidSite('test.stackexchange.com')); // returns 'true'
Even then... this might not be a valid test. Some web servers will implement a catch all rule to re-direct to a 404 page or show a warning, when you try to access a non-existing sub-domain.
Another option is to make a CURL GET
request and make it return the headers only and check if the response header code is 200 (HTTP OK) to verify if the domain/sub-domain is valid. It's up to you how you want to deal with these various scenarios...