开发者

dns_get_record Question

I am setting up a dns lookup开发者_开发知识库 form using dns_get_record. I set it up to check the A Record and MX Records of the domain that is input. However, I would like it to also display the IP address of the displayed MX Records. Is this possible?


No, at least not in one step. You'll have to do another dns request for the "target" of the MX record, which is the "real" address of the mail server.

A simple script could look like this

$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 full blown A and MX record displaying script

$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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜