note 119579 deleted from function.dns-get-record by tularis
| From: | tularis@php.net | Date: | Mon, 11 Jul 2016 19:29:59 +0000 |
| Subject: | note 119579 deleted from function.dns-get-record by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-205967@lists.php.net to get a copy of this message | ||
Note Submitter: pizzafried at gmail dot com
----
I found that this code does not get the correct records all the time (at least not our PHP version).
It has something to do with CNAMES therefore, you should do something like this:
<?php
function dns_get_valid_record($domain, $type=DNS_MX, $root = 0){
$result = dns_get_record("".$domain."", $type); //first try to find it
this way.
if(!$result){
//check for cname;
$result = dns_get_record("".$domain."", DNS_CNAME); //check if it is
a cname, and if so, try that domain.
if($result && $result[0]["target"]){
return dns_get_valid_record($result[0]["target"],$type);
}else{
//if not, translate www.example.com to example.com, and try again.
$domain = explode(".", $domain);
if(count($domain)>2 && $root==0) {
$domain = $domain[count($domain) - 2] . "." . $domain[count($domain) -
1];
return dns_get_valid_record($domain, $type,1);
}
}
}else {
foreach($result as $key=>$record){
if(isset($record["target"]) &&
!filter_var($record["target"], FILTER_VALIDATE_IP)){ //no ip?
if(!dns_get_valid_record($record["target"], DNS_A)){ //we validated
one: check if we get anywhere (A or CNAME=>A)
unset($result[$key]);
}
}
}
}
return $result;
}
?>
you can look up anything else, but this is primarily designed for getting MX records and test if
they could exist. the IP address could still not be in the air or not the correct one, but DNS will
be properly setup when returned results.