Jump to content

Recommended Posts

I have a if statement inside foreach loop with one problem I'm banging my head for a while now.

I would like to test (IF ALL Authoritative DNS servers responded successful to query then print (RESPONSE OK) ONLY ONCE IF ALL SERVERS RESPONDED OK ELSE IF NOT print only server which did not responded).

 

thanks.

 

          
           //////////////////////////
          // Test 1///////////////
          /////////////////////////

              foreach ($response->authority as $k => $result) {

              // Get Authoritative DNS servers 
              $authns = array(gethostbyname($result->nsdname));

              // Initiate Authoritative DNS servers for query
              $dnsservers = new Net_DNS_Resolver(array('nameservers' => $authns));

              // Query Authoritative DNS servers one by one
              $responsedns = $dnsservers->rawQuery($domain);

              // Get an array of DNS Authoritative servers which responded OK
              $resns  = array($responsedns->answerfrom);


// IF ALL Authoritative DNS servers responded successful to query then print (RESPONSE OK) ONLY ONCE 
//  ELSE IF NOT print only server which did not responded)
                     if ($authns == $resns){
                     echo "<font size=2 color=green> (response ok)</font><br>";
                     } else {
                     echo $result->nsdname;
                     }

             }

             }
}

This line is bad. You cannot compare 2 arrays. You must compare values

if ($authns == $resns){

Try this

<?php
$results['success'] = 0;
$results['failure'] = array();
foreach($response->authority as $k => $result) {
// Get Authoritative DNS servers 
$authns = array(gethostbyname($result->nsdname));
// Initiate Authoritative DNS servers for query
$dnsservers = new Net_DNS_Resolver(array('nameservers' => $authns));
// Query Authoritative DNS servers one by one
$responsedns = $dnsservers->rawQuery($domain);
// Get an array of DNS Authoritative servers which responded OK
$resns  = array($responsedns->answerfrom);

    //if ($authns == $resns){
if($authns[0] == $resns[0]) {
	$results['success']++;
    } 
else {
    	$results['failure'][] = $result->nsdname;
}

}
// print the results
print "<h1>Results</h1>";
print "OK Responses: ".$results['success']."<br />";
print "Failed Responses: ".count($results['failure'])."<br />".implode("<br />", $results['failure']);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.