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 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件