How to Ping with PHP
One way to check if a server is available is to ping the server. When you ping a server, you send packets of data to the server, wait for a response and measure the time from transmission to reception. There is a built-in function in PHP to ping a MySQL database server only after a connection to the server has already been established. To ping any other server, you need to use either the "exec" function to shell out to the command line or use the "Net_Ping" class from the PHP Extension and Application Repository (PEAR).
Instructions
-
Ping a MySQL Server
-
1
Create a new PHP file using Notepad or an editor. Open a connection to a MySQL database server. For example, type:
<?php
$dbc = mysql_connect_db("localhost", "username", "password");
-
2
Use the "mysql_ping" function to check if the database server connection is still valid. For example, type:
$ping_result = mysql_ping($dbc);
if ($ping_result)
echo "Connection valid!";
else
echo "Connection not valid!";
?>
-
-
3
Save the file and run it in a browser to test that it functions properly.
Ping Using Exec
-
4
Create a new PHP file in an editor or using Notepad. Declare the host you want to ping as either an IP address or a hostname. Initialize an array to capture the output of the "exec" function and an integer to capture the return value. For example, type:
<?php
$host = "google.com";
$output = array();
$result = null;
-
5
Call the "exec" function to ping the server. Use the "-c" parameter and specify the number of times to ping the server. Pass the array and integer to receive the command output and return value. For example, type:
exec("ping -c 4 " . $host, $output, $result);
-
6
Compare the return value to zero to determine if the ping was successful. Issue an appropriate message. For example, type:
if ($result == 0)
echo "Ping successful!";
else
echo "Ping unsuccessful!";
?>
-
7
Save the file. Run it in a browser and test that it functions properly.
Use the "Net_Ping" Class
-
8
Install the Net_Ping class library from PEAR. Open a command prompt and type:
pear install net_ping-2.4.4
-
9
Create a new PHP file in an editor or Notepad. Include the PEAR class library at the beginning of the script. For example, type:
<?php
require("Net/Ping.php");
-
10
Declare the host you want to ping as either an IP address or hostname. Create a new ping object. For example, type:
$host = "74.125.45.106";
$ping = Net_Ping::factory();
-
11
Ping the host and capture the return output. For example, type:
$result = $ping->ping($host);
-
12
Compare the "_transmitted" to the "_lost" values in the return array to determine if the ping was successful. For example, type:
if ($result["_transmitted"]< $result["_loss"])
echo "Ping successful!";
else
echo "Ping unsuccessful!";
?>
-
13
Save the file and run it in a browser to ensure it functions correctly.
-
1
Tips & Warnings
If you do not receive a ping response from a server, it doesn't necessarily mean the server is down. For security reasons, some servers are configured to not respond to ping requests.