How to ping an IP address using PHP and display results
  John Mwaniki /   05 Jan 2022

How to ping an IP address using PHP and display results

Ping is a TCP/IP command-line utility used to test and verify if a particular destination IP address (a networked device) exists, and can accept requests from another computer/device within the network.

It's usually used to test if a computer can communicate over the network with another computer or network device.

The ping command verifies the connection by sending Internet Control Message Protocol (ICMP) Echo Request packets (messages) to a specific IP address on a network and waits for the remote host to respond with a reply packet. The corresponding echo Reply messages and how long it takes for them to return, are the two important pieces of information that are displayed as the result.

By default, the ping command sends four echo requests in Windows (Linux continues to send until you stop it using Ctrl + C). The result of each of these echo requests is displayed, which includes:

  • Whether the request received a successful response
  • How many bytes were received in response
  • The Time to Live (TTL)
  • How long it takes to receive the response
  • Statistics about packet loss and round trip times.

If the ping request does not get a response, then the network is broken and something needs to be fixed. If it takes too long to get back a response, then that means that the connection is slow.

This command can also be used to test both the computer name (or domain name) and the IP address of the computer/server. If pinging the IP address is successful, but pinging the computer name isn't, there might be a name resolution problem.

By default, the ping command is run in a command line like the command prompt (for Windows) and terminal (for Linux).

Making a Ping request

To make a ping request, simply write the word "ping" followed by a space then the IP address or domain name whose connection you want to test in the command prompt or terminal. Then hit enter.

Example 1

ping google.com

Output:

Pinging a domain name via linux terminal

Example 2

ping 172.217.170.174

Output:

Pinging an IP address via linux terminal

Limiting the number of ping requests

The ping command allows additional options when sending the ping request. These options might differ from one operating system to another.

We will only cover one option as command options are not in the scope of this article.

To specify the number of ICMP Echo Requests to send, use "-n" (for Windows), and "-c" (for Linux). The default number of requests is 4 in Windows if this option is not used.

Below are examples of how to -n and -c on Windows and Linux respectively.

Setting ping requests limit in Windows

ping -n 5 172.217.170.174

Output:

Pinging 172.217.170.174 with 32 bytes of data:
Reply from 172.217.170.174: bytes=32 time=11ms TTL=118
Reply from 172.217.170.174: bytes=32 time=10ms TTL=118
Reply from 172.217.170.174: bytes=32 time=66ms TTL=118
Reply from 172.217.170.174: bytes=32 time=27ms TTL=118
Reply from 172.217.170.174: bytes=32 time=54ms TTL=118

Ping statistics for 172.217.170.174:
Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 10ms, Maximum = 66ms, Average = 33ms

Setting ping requests limit in Linux

ping -c 5 172.217.170.174

Output:

PING 172.217.170.174 (172.217.170.174) 56(84) bytes of data.
64 bytes from 172.217.170.174: icmp_seq=1 ttl=118 time=11.7 ms
64 bytes from 172.217.170.174: icmp_seq=2 ttl=118 time=8.97 ms
64 bytes from 172.217.170.174: icmp_seq=3 ttl=118 time=64.3 ms
64 bytes from 172.217.170.174: icmp_seq=4 ttl=118 time=29.9 ms
64 bytes from 172.217.170.174: icmp_seq=5 ttl=118 time=30.3 ms

--- 172.217.170.174 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 8.969/29.018/64.322/19.752 ms

Making Ping command in PHP

For some reason, you may want (or may have) to do a ping command from your PHP application.

For instance, your website/application may be sending API requests to an address on a remote server. You of course expect to get back a response (either a success or error message) to the API request. But in some cases, you may fail to get any. In such a scenario, you can use the ping request to test the network connection between your web server and the remote server.

If you send the ping request using the command prompt or terminal in your PC, then you will be testing the network connection between your network IP and the remote host (and not between the web server and the remote host).

The ping request needs to originate from the server. You can send the request from a piece of code running on the web server.

PHP, being a server-side language, is an excellent way of running a ping command from your webserver to another IP address or domain name on the Internet.

In PHP, the ping command is the same as in the terminal/command prompt, only it is executed within the exec() function.

The exec() is an in-built PHP function that enables the execution of an external program.

Syntax

exec($command, $output, $result)

Parameters

Parameter Requirement Description
$command Required This specifies the actual command to be executed.
$output Optional This variable holds an array of every line of output from the command.
$result Optional This variable holds the return status of the executed command.

The function returns the last line from the result of the command.

Example 1

Sending 3 ICMP Echo Request packets to google.com from a PHP script on a Linux server.

<?php
echo exec("ping -c 3 www.google.com");

Output

rtt min/avg/max/mdev = 0.370/0.395/0.425/0.032 ms

Example 2

Sending 5 ICMP Echo Request packets to google.com from a PHP script on a Windows server.

<?php
echo exec("ping -n 5 www.google.com");

Output

Minimum = 10ms, Maximum = 66ms, Average = 33ms

Example 3

<?php
$ip = "172.217.170.174";
exec("ping -c 3 $ip", $output, $result);
print_r($output);

Output

Array (
[0] => PING 172.217.170.174 (172.217.170.174) 56(84) bytes of data.
[1] => 64 bytes from 172.217.170.174: icmp_seq=1 ttl=119 time=143 ms
[2] => 64 bytes from 172.217.170.174: icmp_seq=2 ttl=119 time=143 ms
[3] => 64 bytes from 172.217.170.174: icmp_seq=3 ttl=119 time=160 ms
[4] => [5] => --- 172.217.170.174 ping statistics ---
[6] => 3 packets transmitted, 3 received, 0% packet loss, time 2002ms
[7] => rtt min/avg/max/mdev = 143.342/148.966/160.196/7.940 ms
)

Example 4

<?php
exec("ping -c 3 facebook.com", $output, $result);
foreach($output as $response){
 echo $response."
"; }

Output

PING facebook.com (157.240.9.35) 56(84) bytes of data.
64 bytes from edge-star-mini-shv-01-sof1.facebook.com (157.240.9.35): icmp_seq=1 ttl=59 time=0.257 ms
64 bytes from edge-star-mini-shv-01-sof1.facebook.com (157.240.9.35): icmp_seq=2 ttl=59 time=0.278 ms
64 bytes from edge-star-mini-shv-01-sof1.facebook.com (157.240.9.35): icmp_seq=3 ttl=59 time=0.242 ms

--- facebook.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.242/0.259/0.278/0.014 ms

Note: When allowing user inputs to the exec() function, always use escapeshellarg() or escapeshellcmd() functions to ensure that users cannot trick the system into executing arbitrary commands.

The exec() function poses a potential security hole and therefore some hosting companies may have disabled it by default because of this. If it fails to work for you, this could be the main cause.

It may give you an error message below:

Warning: exec() has been disabled for security reasons in /path/to/file/filename.php on line x

Or for PHP 8, it may give the undefined function error below:

Fatal error: Uncaught Error: Call to undefined function exec() in /path/to/file/filename.php:X Stack trace: #0 {main} thrown in /path/to/file/filename.php on line X

Conclusion

In this article, we have covered what a ping command is, what it is used for, and how it works.

We also covered how to make ping commands in PHP using the in-built exec() function with the aid of multiple examples.