dns_get_record问题

时间:2010-06-08 20:30:53

标签: php

我正在使用dns_get_record设置dns查找表单。我将其设置为检查输入域的A记录和MX记录。但是,我希望它还显示所显示的MX记录的IP地址。这可能吗?

1 个答案:

答案 0 :(得分:1)

不,至少不是一步到位。您将不得不对MX记录的“目标”执行另一个dns请求,这是邮件服务器的“真实”地址。

一个简单的脚本可能看起来像这样

$email = "anyone@staff.example.com";
list( $tmp, $email ) = explode( "@", $email );  // Gets the domain name

$dns = dns_get_record( $email, DNS_MX );
if( count($dns) <= 0 )
    die( "Error looking up dns information." ); // Return value is an empty array if there aren't any MX records but domain exists

// Looks up the first returned MX (note that there can be more than one)
// Each MX record has a 'pri' value where the lowest value is the record with the highest priority
$mx = dns_get_record( $dns[0]['target'], DNS_A );
if( count($mx) <= 0 )
    die( "Error looking up mail server." );
$mx = $mx[0]['ip'];

完整的A和MX记录显示脚本

$domain = "google.com";

$dns = dns_get_record( $domain, DNS_ANY );
foreach( $dns as $d ) {
    // Only print A and MX records
    if( $d['type'] != "A" and $d['type'] != "MX" )
        continue;
    // First print all fields
    echo "--- " . $d['host'] . ": <br />\n";
    foreach( $d as $key => $value ) {
        if( $key != "host" )    // Don't print host twice
            echo " {$key}: {$value} <br />\n";
    }
    // Print type specific fields
    switch( $d['type'] ) {
        case 'A':
            // Display annoying message
            echo "A records always contain an IP address. <br />\n";
            break;
        case 'MX':
            // Resolve IP address of the mail server
            $mx = dns_get_record( $d['target'], DNS_A );
            foreach( $mx as $server ) {
                echo "The MX record for " . $d['host'] . " points to the server " . $d['target'] . " whose IP address is " . $server['ip'] . ". <br />\n";
            }
            break;
    }
}