How to ping an IP address using PHP

ping an IP address using PHP

Ping serves as a TCP/IP command-line tool essential for testing and confirming the existence of a specific destination IP address, typically its representing a networked device. Its primary purpose is to ascertain whether this target device can acknowledge and process requests from other computers or devices within the network infrastructure.

It’s commonly used to verifying if a computer can communicate over the network with another computer or network device across the network.

To verify the connection, the ping command dispatches Internet Control Message Protocol (ICMP) Echo Request packets to a designated IP address within the network, awaiting the remote host’s response in the form of a reply packet. The essential components of this interaction are the echo Reply messages and their respective round-trip times, crucial pieces of information displayed as the outcome of the ping operation.

By default, in Windows, the ping command sends four echo requests, while in Linux, it continues sending until halted using Ctrl + C. The outcome of each echo request is meticulously displayed, providing details such as:

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

Failure to receive a response to a ping request indicates a network issue requiring attention and resolution. Conversely, a delayed response signifies a slow connection, prompting the need for optimization measures to improve network performance.

Additionally, this command facilitates testing both the computer name (or domain name) and the IP address of the computer/server. If pinging the IP address yields success while pinging the computer name fails, it could indicate a potential name resolution issue.

Read Also:  [PHP]: Various Techniques to Modify Array Values

The ping command is typically executed in a command-line interface, such as the Command Prompt in Windows or the Terminal in Linux, by default.

Making a Ping request

To make a ping request, simply open the command-line interface on your system (Command Prompt for Windows or Terminal for Linux), then type “ping” followed by the IP address or domain name you want to ping.

For example:





After entering the command, press Enter, and the ping utility will send ICMP echo requests to the specified destination, displaying the results in real-time.

Limiting the number of ping requests

Additional options are available with the ping command when sending a ping request, and these options can vary between operating systems.

We will focus solely on one option, as command options fall outside the scope of this article.

To determine the quantity of ICMP Echo Requests to transmit, utilize “-n” for Windows and “-c” for Linux. If this option remains unused, Windows defaults to 4 requests.

Here are examples of how the usage of “-n” in Windows and “-c” in Linux:


ping -n 6


ping -c 6


Making Ping command in PHP

In some cases, you may want (or may have) to do a ping command from within your PHP application.

For example, your website or application might regularly send API requests to a remote server address. While you anticipate receiving a response (either a success or error message) there may be occasions when no response is received. In such instances, employing a ping request can help diagnose the network connection between your web server and the remote server.

Read Also:  [PHP]: Various Techniques to Modify Array Values

If you execute the ping request via the command prompt or terminal on your PC, you’ll test the network connection between your PC’s network IP and the remote host, rather than between the web server and the remote host.

For accurate testing of the network connection between your web server and the remote host, the ping request must originate from the server itself.

Given that PHP operates server-side, it provides an effective means to execute a ping command directly from your web server to another IP address or domain name on the Internet.

In PHP, the ping command mirrors that of the terminal/command prompt, albeit executed within the exec() function.

The exec() function is a built-in feature of PHP designed to facilitate the execution of an external program.


exec($command, $output, $result)


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 executed.

Example 1

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

echo exec("ping -c 3");


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

Example 2

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

echo exec("ping -n 5");


Read Also:  [PHP]: Various Techniques to Modify Array Values


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

Example 3

$ip = "";
exec("ping -c 3 $ip", $output, $result);


Array (
[0] => PING ( 56(84) bytes of data.
[1] => 64 bytes from icmp_seq=1 ttl=119 time=143 ms
[2] => 64 bytes from icmp_seq=2 ttl=119 time=143 ms
[3] => 64 bytes from icmp_seq=3 ttl=119 time=160 ms
[4] => [5] => --- 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

exec("ping -c 3", $output, $result);
foreach($output as $response){
 echo $response."";


PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=59 time=0.257 ms
64 bytes from ( icmp_seq=2 ttl=59 time=0.278 ms
64 bytes from ( icmp_seq=3 ttl=59 time=0.242 ms

--- 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: Always employ the escapeshellarg() or escapeshellcmd() functions when permitting user inputs to the exec() function. This precaution is vital to prevent potential exploitation by malicious users attempting to execute arbitrary commands.

The exec() function can be considered a potential security vulnerability, leading some hosting companies to disable it by default for this reason. If you encounter difficulties using it, this may be the primary cause.

You may encounter the following error message:

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

For PHP 8, you may receive the following error message:

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


In this article, we’ve explored the fundamentals of the ping command, its purpose, and functionality.

Additionally, we’ve delved into executing ping commands in PHP through the built-in exec() function, accompanied by numerous illustrative examples.

maxwin slot mahjong ways