drymoeuka282427675 2010-06-08 20:30
浏览 127
已采纳

dns_get_record问题

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?

  • 写回答

1条回答 默认 最新

  • douluan8828 2010-06-08 20:39
    关注

    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 />
    ";
        foreach( $d as $key => $value ) {
            if( $key != "host" )    // Don't print host twice
                echo " {$key}: {$value} <br />
    ";
        }
        // Print type specific fields
        switch( $d['type'] ) {
            case 'A':
                // Display annoying message
                echo "A records always contain an IP address. <br />
    ";
                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 />
    ";
                }
                break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡