PHP Ping

Ask about a PHP problem here.
Post Reply
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

PHP Ping

Post 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?
[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]
Last edited by Helx on Tue Nov 13, 2012 11:28 am, edited 1 time in total.
Reason: Not too sure if you put a real IP or an internal one... Would rather not risk it :)
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP Ping

Post 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 :)
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

Re: PHP Ping

Post by dareskog »

i was just using xammp on my pc for now, how do i enable this on there?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: PHP Ping

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Ping

Post by jacek »

That looks like the Linux ping command, if you are running Windows it's not going to work :P
Image
Post Reply