How to ping a server using PHP?

How do I use PHP to get the PING of a server?

I actually want to ping several servers, and then echo the outputs in a list (arranged by the lowest to highest).

With the system() function?
string system ( string command [, int return_var])
http://www.php.net/manual/en/function.system.php

Hm, not sure how to use that function. And the manual doesn’t seem to explain what “string command” and return_var can be. Tried a inserting a few things but I can’t get it to work. Anyone?

Example:


<?
system("ping www.example.com");
?>

…results in this being written…


Pinging www.example.com [192.0.34.166] with 32 bytes of data:



Reply from 192.0.34.166: bytes=32 time=191ms TTL=43

Reply from 192.0.34.166: bytes=32 time=190ms TTL=43

Reply from 192.0.34.166: bytes=32 time=180ms TTL=43

Reply from 192.0.34.166: bytes=32 time=180ms TTL=43



Ping statistics for 192.0.34.166:

    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

    Minimum = 180ms, Maximum =  191ms, Average =  185ms

Hmm, tried yours, but nothing showed up at all on the page.

Also found this on Google:

[font=Courier]

<?
$gamehost = "66.218.71.198"; 
$result = system("ping -c 1 $gamehost" ); [/font]
[font=Courier]echo "<CENTER>"; 
echo "Your ping to our network is $result ms"; 
echo "</CENTER>"; 
?>

[/font]

Which results in “Your ping to our network is ms”.

I basically want to ping three different servers and echo the results like this (example):

Yahoo.com 26
Amazon.com 19
Widgets.com 41

Don’t need all of the results, just an average ping of the server.

Any ideas?

Edit: Ignore the font tags in previous code, the forum is being buggy for me.

This worked fine for me on my dev and live servers, but it might not work if you’re running PHP in safe mode. Also, the forum ate my line breaks.

function ping($addresses)
{
	foreach ($addresses as $a)
	{
		$ping = exec('ping '.gethostbyname($a));
		$chunks = explode(' ', $ping);
		$avg = substr($chunks[14], 0, strlen($chunks[14])-2);
		
		$results[$a] = $avg;
	}
	asort($results);
	echo '<table border="1" cellpadding="4">'."\
";
	while (list($key, $val) = each($results))
	{
		echo '<tr>'."\
";
		echo '  <td align="right"><strong>'.$key.'</strong></td>'."\
";
		echo '  <td>'.$val.'ms</td>'."\
";
		echo '</tr>'."\
";
	}
	echo '</table>'."\
";;
}
$sites = array(
	'mattjacob.org',
	'sitepoint.com',
	'yahoo.com',
	'google.com',
	'apple.com',
	'ebay.com'
);
ping($sites);

Nice little ping() function :slight_smile:
Only objection I have; why not return $results from ping(), and do the “format as html table” in another function?

Off Topic:

No, not the forum, The BLOB ate your line breaks :smiley:

Don’t forget though that on some systems ping may be locked down to root user only. A better way of checking would be to open a socket to the server - that way you can actually see if services are responding too. ping will work at times even when the server is not responding to requests on any services, so it gives a false sense of security.

Thanks,

Well damn, looks like my server is running in safemode since none of those work :frowning:

What’s the point of safemode anyway. How does it make the server more secure, etc. Do most of you hosts around here use that?

Good point. A function really should only serve one specific purpose, but when you’re in a hurry… :stuck_out_tongue: