php funciton problem

Posted by Holian on Stack Overflow See other posts from Stack Overflow or by Holian
Published on 2010-05-21T07:27:11Z Indexed on 2010/05/21 7:30 UTC
Read the original article Hit count: 272

Filed under:
|
|

Hello,

i have this little script, which should ping the IPs in the host_to_ping array. This php is called with JS in the index.html.

But somethin' wrong, 'coz the $rval is always 1 (which mean the host is unreachable). But i know that the first two host is alive.

So i print the $res variable, and i see the message: Need to give the IP. I dont understand why dont replace the $host variable to the actual IP address in the function.

<?php
  function ping($host) {
  exec(sprintf('ping -n 4', escapeshellarg($host)), $res, $rval);
    print_r($res);
    return $rval === 0;
  }

  $hosts_to_ping = array('10.54.23.254', '10.22.23.254', '10.23.66.134');
?>

<ul>
<?php foreach ($hosts_to_ping as $host): ?>
  <li>
  <?php echo $host; ?>
  <?php $up = ping($host); ?>

    (<img src="<?php echo $up ? 'on' : 'off'; ?>"
          alt="<?php echo $up ? 'up' : 'down'; ?>">)
  </li>
<?php endforeach; ?>
</ul>

© Stack Overflow or respective owner

Related posts about php

Related posts about function