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

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

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.

Related Searches:

References

Comments

You May Also Like

  • How to use Ping

    Pinging Internet connections can be useful in network diagnostics, or when testing the speed of an Internet connection. Windows has a utility...

  • How to Create a Ping Service Website

    Ping is a popular networking utility that is used for the purpose of establishing whether or not a host is reachable on...

  • How to Ping Another Computer on a Wireless Hub

    When you are connected to a wireless hub, your computer is able to "see" the other computers in the network because they...

  • Network Ping Software

    Network ping software allows you to ping network devices such as servers, switches and routers. This software can log statistics such as...

  • How to Find Loopback on a Cisco

    Cisco network devices support loopback interfaces for testing purposes as well as special configurations that can only function when a virtual loopback...

  • How to Configure Two Cisco Routers to Connect

    Routers direct information flow in a network. A router determines the destination for the information that enters it, as well the quickest...

  • How to Measure Wireless Packet Loss

    Anytime your computer is making a connection with another computer or device, a continuous stream of data is transferring between the computer...

  • How to Ping an IP Address

    The ability to ping an IP address is vital to network troubleshooting. Ping utilizes the ICMP ECHO protocol and simply sends a...

  • How to Write a Script to Ping Every 5 Minutes

    The ping command sends several small packets of information from your computer to the designated target in an effort to determine if...

  • How to Do a Ping Test

    While there are many ways to determine if the network connection on your computer is working, a ping test allows you to...

Related Ads

Featured