0

Hi I am trying to incorporate a function in my project that will ping my app servers and see if they are running or not. Problem is, everytime I try to use this function it always returns that they are alive, even though I am in my local environment, and there are no servers at those IP addresses. So whats going on?

Here is my function:

function ping($host, $port, $timeout) { 
    if($fp = fsockopen($host,$port,$errCode,$errStr,$timeout)){   
       return 'Alive';
    } else {
       return 'Not alive'; 
    } 
    fclose($fp);
}

Here is how I am calling the function:

    foreach ($appservers as $key => $value) {
        echo ping($value['privateip'] , 80, 1).'<br/>';
    }

Here is my array of server variables:

$appservers = array(
'app1' => array(
    'publicdns' => 'app1.xxxx.com',
    'privateip' => '1.1.1.101',
    'alive' => NULL,
    'active_users' => NULL
),

'app2' => array(
    'publicdns' => 'app2.xxxx.com',
    'privateip' => '1.1.1.102',
    'alive' => NULL,
    'active_users' => NULL
),

'app3' => array(
    'publicdns' => 'app3.xxxx.com',
    'privateip' => '1.1.1.103',
    'alive' => NULL,
    'active_users' => NULL
),

'app4' => array(
    'publicdns' => 'app4.xxxx.com',
    'privateip' => '1.1.1.104',
    'alive' => NULL,
    'active_users' => NULL
)

);

slick1537
  • 745
  • 2
  • 8
  • 19

1 Answers1

0

Have see this issue before , You are using a DNS service that resolves non-existent domains to a server that gives you a "friendly" error page, which it returns with a 200 response code.

See : get_headers Inconsistency for more details of this issue.

You can resolve it with DavidRandom solution

var_dump(ping("stackoverflow.com", 30, 1)); // port 30 return false
var_dump(ping("stackoverflow.com", 80, 1)); // port 80; returns 0.49000000953674

Your Function Modified

function ping($host, $port, $timeout) {
    $a = gethostbyname('idontexist.tld');
    $b = gethostbyname($host);

    if ($a == $b)
        return false;

    $time = microtime(true);
    $fp = @fsockopen($host, $port, $errCode, $errStr, $timeout);
    $time = microtime(true) - $time;
    if ($fp) {
        fclose($fp);
        return $time;
    } else {
        return false;
    }
}
Community
  • 1
  • 1
Baba
  • 94,024
  • 28
  • 166
  • 217
  • awesome thanks for the update. Just wondering what the @ sign before the fsockopen does? – slick1537 May 08 '13 at 12:04
  • Bad news is I just tried the updated code and it is returning ping times....`0.010555028915405 0.010455846786499 0.010455846786499 0.0095298290252686` – slick1537 May 08 '13 at 12:08
  • @KevinMcKinney error supression .... If you can give 2 of this IP i would like to test myself – Baba May 08 '13 at 12:11
  • the IPs I am using are listed in the $appservers array. 1.1.1.101, 1.1.1.102, 1.1.1.103, and 1.1.1.104 – slick1537 May 08 '13 at 12:15
  • In the end I can just 'ping' it on a different port that would normally be open if the server application was running, which is really what I am checking for anyway. – slick1537 May 10 '13 at 12:13