PHP Ping
Posted: Mon Nov 12, 2012 6:57 pm
I stumbled across this code and tried running it but it always seems to return "down" or "off" - Can anyone see whats wrong?
[syntax=php]
<?php
function ping($host) {
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
return $rval === 0;
}
$hosts_to_ping = array('RANDOM_IP', '127.0.0.1');
?>
<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>
[/syntax]
[syntax=php]
<?php
function ping($host) {
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
return $rval === 0;
}
$hosts_to_ping = array('RANDOM_IP', '127.0.0.1');
?>
<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>
[/syntax]