checkdnsrr

(PHP 4, PHP 5, PHP 7, PHP 8)

checkdnsrr给指定的主机(域名)或者IP地址做DNS通信检查

说明

checkdnsrr(string $hostname, string $type = "MX"): bool

在 DNS 中搜索与 hostname 对应的 type 类型纪录。

参数

hostname

hostname 可以是点分十进制的 IP 地址,也可以是主机名。

type

type 可能是下面这些类型中的任何一个:A、MX、NS、SOA、PTR、CNAME、AAAA、A6、SRV、NAPTR、TXT 或 ANY。

返回值

如果记录能找到,就返回 true;如果查找不到该 DNS 记录或者发生了错误,就返回 false

注释

注意:

出于对低版本在 Windows 平台上的兼容性,可以试试 » PEAR 扩展包里面提供的 » Net_DNS 类。

参见

添加备注

用户贡献的备注 3 notes

up
34
Krisztin Ferenczi
11 years ago
criffoh at gmail dot com is right. Before you check domain, you must convert to ascii with idn_to_ascii function:
http://us2.php.net/manual/en/function.idn-to-ascii.php .

var_dump(checkdnsrr('ñandu.cl', 'A')); // returns false
var_dump(checkdnsrr(idn_to_ascii('ñandu.cl'), 'A')); // return true
up
16
Martin
8 years ago
Important Warning:

You need to add a dot to the end of the host name to make a fully qualified domain name.

To see why, try executing the following pieces of code:

$d1="gmail.con";
$d2="gmail.con.";
$r1=checkdnsrr($d1, "MX");
$r2=checkdnsrr($d2, "MX");
var_dump($r1);
var_dump($r2);

You will see that without the dot it claims that the domain "gmail.con" is valid.

Note that if you time the "checkdnsrr()" calls you will also see it takes longer without the dot because it treats it as a relative domain and does several tries based on the host name it is running on.

NB: in case you're interested, being treated as a relative domain explains what is happening. If your host is "example.com" the relative domain will eventually resolve to "gmail.con.com." which can be looked up, hence it wrongly claims "gmail.con" exists
up
1
n at zay dot uk
3 years ago
I always think you should state the blindingly obvious.
A function that contains checkdnsrr will return false without an internet connection.
So in a production environment you need an active internet connection to return a true result with a valid email address.
To Top