php教程

checkdnsrr

(PHP 4, PHP 5, PHP 7)

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

说明

checkdnsrr ( string $host [, string $type = "MX" ] ) : bool

根据不同记录(type)类型查询主机(host)相应的DNS记录。

参数

host

主机(host)可以是一个IP地址也可以是域名。

type

解析记录类型(type)可能是下面这些类型中的任何一个:A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY。

返回值

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

更新日志

版本 说明
5.3.0 这个函数在Windows平台上也可以使用了。
5.2.4 增加了TXT的记录类型
5.0.0 增加了AAAA的记录类型

注释

Note:

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

参见

User Contributed Notes

dobs 24-Jul-2018 09:04
$this->_buf - for bulk check cache

function ValidateEmail($email)
    {
        list($user, $domain) = explode('@', $email);

        if (!filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE)
            return 0;

        if (isset($this->_buf[$domain]))
            return $this->_buf[$domain];

        if (checkdnsrr($domain) === FALSE) {
            $this->_buf[$domain] = 0;
            return 0;
        }
        $this->_buf[$domain] = 1;

        return 1;
    }
Martin 30-Sep-2016 10:02
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
Krisztin Ferenczi 27-Oct-2013 11:12
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
criffoh at gmail dot com 18-Jul-2013 02:57
Is not possible validate domains with '?' for my country.

In my country is possible to register domain using '?' character. For example:

?andu.cl
http://nic.cl/cgi-bin/dom-CL?q=%F1andu

If I use this function to check DNS record, it return false, but the domain already exists:

var_dump(checkdnsrr('?andu.cl', 'A')); // returns false
Patrick 13-Dec-2004 07:53
This is a little code example that will validate an email address in two ways:
- first the general syntax of the string is checked with a regular expression
- then the domain substring (after the '@') is checked using the 'checkdnsrr' function

<?php

function validate_email($email){

  
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

   if(
eregi($exp,$email)){

      if(
checkdnsrr(array_pop(explode("@",$email)),"MX")){
        return
true;
      }else{
        return
false;
      }

   }else{

      return
false;

   }   
}

?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788