How to Ping Using PHP

Pinging a server with PHP is useful when you want to check if a server is online or to measure the time it takes the server to receive and send back a packet. The ping utility sends a ICMP echo request packet to the server and waits for an ICMP response. Ping output includes packet loss and the minimum, maximum and average round-trip time of the response packets. Accessing shell commands in PHP is done through the "shell_exec" function.

Instructions

    • 1

      Open the PHP file in a text editor, such as Windows Notepad.

    • 2

      Ping a server with the "shell_exec" function by adding the code "$result = shell_exec('ping server.com');" in the body of your file at the point where you want to ping the server. The "shell_exec" function lets you execute a shell command and returns the output as a string. Another way to perform the same action as "shell_exec" is with backticks (``), such as "$result = `ping server.com`;" Replace "server.com" with the IP address you want to ping.

    • 3

      Display the ping output by adding the code "echo "<pre>$result</pre>";" on the line after you call the "shell_exec" function. A line of output from the ping command will be similar in format to "64 bytes from myserver.com (156.11.111.5): icmp_seq=2 ttl=50 time=83.0 ms."

    • 4

      Save the PHP file, and load it on your server.

Tips & Warnings

  • PHP code needs to be stored inside "<?php" and "?>" tags.

Related Searches:

References

Comments

Related Ads

Featured