Page 1 of 1

PHP Ping

Posted: Mon Nov 12, 2012 6:57 pm
by dareskog
I stumbled across this code and tried running it but it always seems to return "down" or "off" - Can anyone see whats wrong?
<?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>

Re: PHP Ping

Posted: Tue Nov 13, 2012 5:05 am
by Helx
Are you hosting this yourself or do you use a professional/free host?
Most hosts disable those commands due to malicious activities.

By the way, works fine for me :)

Re: PHP Ping

Posted: Tue Nov 13, 2012 8:19 pm
by dareskog
i was just using xammp on my pc for now, how do i enable this on there?

Re: PHP Ping

Posted: Tue Nov 13, 2012 11:36 pm
by Helx
Just look for your php.ini file, there are a bunch of options in there.
I believe you need to look for "Safe_mode" or something.

XAMPP, isn't that program limited to local connections only?
You may have to allow it to bypass firewalls and whatnot if you want outbound connections.

Re: PHP Ping

Posted: Sat Nov 17, 2012 12:29 am
by jacek
That looks like the Linux ping command, if you are running Windows it's not going to work :P