How to Ping with PHP

Written by steve mcdonnell Google
  • Share
  • Tweet
  • Share
  • Email

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

Skill level:
Moderate

Other People Are Reading

Instructions

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

    Save the file and run it in a browser to test that it functions properly.

  1. 1

    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. Initialise 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;

  2. 2

    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);

  3. 3

    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!";

    ?>

  4. 4

    Save the file. Run it in a browser and test that it functions properly.

  1. 1

    Install the Net_Ping class library from PEAR. Open a command prompt and type:

    pear install net_ping-2.4.4

  2. 2

    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");

  3. 3

    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();

  4. 4

    Ping the host and capture the return output. For example, type:

    $result = $ping->ping($host);

  5. 5

    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!";

    ?>

  6. 6

    Save the file and run it in a browser to ensure it functions correctly.

Tips and 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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.