tags:

views:

56

answers:

1

I am using a PHP script that makes use of dns_get_record to check the A and MX records of domains. The domain is entered via a simple form. However, I'm having some issues adding 'www' to the domain variable.

I would like to add an A Record lookup for www.domain.com. How do I add the www?

<?php

$domain = $_POST["Domain"];

$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 "For " . $d['host'] . ": <br />\n";
//    foreach( $d as $key => $value ) {
//        if( $key != "host" )    // Don't print host twice
//            echo " {$key}: <b>\n {$value}</b>\n <br />\n";
//     }
    // Print type specific fields
    switch( $d['type'] ) {
        case 'A':
            // Display annoying message
            echo "<b>\n" . $d['ip'] . "</b>\n is the Primary A Record for this domain. <br /><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 <b>\n" . $d['target'] . "</b>\n whose IP address is <b>\n" . $server['ip'] . "</b>. It has a priority of <b>\n" . $d['pri'] . "</b>\n. <br /><br />\n";
            }
        if ( $d['target'] == $domain ) {
            echo "<i>It looks like the domain is using itself as an MX Record.  You will need to create additional records.</i><br /><br />\n";
                } else {
            echo "<i>This MX Record looks fine.</i><br /><br />\n";
            }
            break;
    }
}

error_reporting(E_ALL);

?>
+1  A: 

I would suggest putting the code into a function:

function getDNSRecord($domain) {
$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 "For " . $d['host'] . ": <br />\n";
//    foreach( $d as $key => $value ) {
//        if( $key != "host" )    // Don't print host twice
//            echo " {$key}: <b>\n {$value}</b>\n <br />\n";
//     }
    // Print type specific fields
    switch( $d['type'] ) {
        case 'A':
            // Display annoying message
            echo "<b>\n" . $d['ip'] . "</b>\n is the Primary A Record for this domain. <br /><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 <b>\n" . $d['target'] . "</b>\n whose IP address is <b>\n" . $server['ip'] . "</b>. It has a priority of <b>\n" . $d['pri'] . "</b>\n. <br /><br />\n";
            }
        if ( $d['target'] == $domain ) {
            echo "<i>It looks like the domain is using itself as an MX Record.  You will need to create additional records.</i><br /><br />\n";
                } else {
            echo "<i>This MX Record looks fine.</i><br /><br />\n";
            }
            break;
    }
}
}

And then call said function twice:

getDNSRecord($_POST['Domain']);
getDNSRecord('www.'.$_POST['Domain']);
robinjam
@Robinjam - Thanks for this. Although, when I add this, all I am getting is 'Array'. Thoughts?
Batfan
@Robinjam - Also, I am using DNS_ANY so that I can call on specific parts of the generated array (like the IP address)
Batfan
I see. What do you mean by "all I am getting is 'Array'"?
robinjam
I am placing the following code, 2 lines above the error reporting line (see code above) -------------- $domain2 = 'www.'.$_POST['Domain'];$domain2r = dns_get_record( $domain2, DNS_A );echo $domain2r; ---------------------When the page loads, at the bottom, only the word 'Array' is displayed.
Batfan
That's because dns_get_record() returns an array. I intended for you to replace the first line of your script with the code I suggested, rather than adding it to the end ;)
robinjam
@Robinjam - Ha, okay! The only problem with that though is that I need to check both the primary A Record (using just the domain) AND the 'www' record. In other words, an A Record lookup on domain.com and www.domain.com
Batfan
Oh I see! Give me a few minutes to edit my answer ;)
robinjam
@Robinjam - Sorry I wansnt being clear before :) -- I went ahead and tried the new code and I'm getting this error ---- Warning: Unexpected character in input: '\' (ASCII=92) state=1 on line 15; ----------- line 15 is just \n";
Batfan
Sorry, Stack Overflow's treating the br's as returns and it's messing up my answer.
robinjam
OK, I've fixed it now. Try it again.
robinjam
@Robinjam - I figured that was probably the case. I tried this new code and it seems to be giving me the same results I had before. I dont see anything for the 'www' record. Am I missing something? Here is a live example -- http://bit.ly/bdGUR8
Batfan
I ran it on my local server against google.com, and it gave me two different sets of results. Are you sure you copied the code correctly?
robinjam
@Robinjam - Hmmm, weird. Is it working for you when you try the live example I posted?
Batfan
Nope, your example only gives me 1 set of results. Sorry, I'm really not sure what's stopping your code from working :/
robinjam
@Robinjam - Well crap. I posted a txt version of the script I'm using. Thoughts? --- http://bit.ly/amZovy
Batfan
No idea, sorry. Your script is the exact same as mine.
robinjam
Crap. Guess I'm pretty much where I started off :P
Batfan