April 24th, 2006

Ping a Server/Remote Host With PHP

I had previously posted the code to ping a server or remote host from an ASP page. Well no doubt there are many PHP programmers that would like to be able to ping from a PHP page or web application as well. This would be handy for testing up-time and server status. The code also includes code to prevent form spoofing.

The function also uses a regular expression to validate the IP address format.

You can use CSS to style the output, make it look like a command prompt if you want!


<?php
// Address error handling.
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);

// Obtain POST data.
$ping_ip_addr = $_POST['ping_ip_addr'];
$ping_count   = $_POST['ping_count']; 

// Remove any slashes
if (get_magic_quotes_gpc())
    $ping_ip_addr = stripslashes($ping_ip_addr);

// Create arrays.
$ping_count_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25); // Ping count
?>
<html>
<head>
<title>Ping</title>
</head>
<body>
<h1>Ping</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="ping_ip_addr">IP address:</label><br />
<input name="ping_ip_addr" id="ping_ip_addr" type="text" value="<?php echo $_POST['submit'] == 'Ping' ? htmlentities($ping_ip_addr, ENT_QUOTES) : $_SERVER['REMOTE_ADDR'];; ?>" size="40" maxlength="15" /></p>
<p><label for="ping_count">Ping count:</label><br />
<select name="ping_count" id="ping_count">
<?php
foreach ($ping_count_array as $ping_count_item)
    {
    echo '<option' . ($ping_count == $ping_count_item ? ' selected="selected"' : '') . '>' . $ping_count_item . '</option>' . "\n";
    }
?>
</select></p>
<p><input type="submit" name="submit" value="Ping" /></p>
</form>
<p>Ping may take a while, please be patient.</p>
<?php

if ($_POST['submit'] == 'Ping')
    {
    echo '<div class="output">' . "\n";

    // Check for spoofed form submission.
    $illegal = FALSE;

    if (strlen($ping_ip_addr) > 15)
        $illegal = TRUE;

    if (!in_array($ping_count, $ping_count_array))
        $illegal = TRUE;

    if (!$illegal) // Form submission was not spoofed.
        {
        if (ereg('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', $ping_ip_addr)) // Acquired data contains no problems.
            {
            // Display result.
            echo '<pre>' . "\n" .
                 'ping -c ' . $ping_count . ' ' . $ping_ip_addr . "\n\n";
            system('ping -c ' . $ping_count . ' ' . $ping_ip_addr); // Ping IP address.
            echo '</pre>' . "\n" .
                 '<p>Ping complete.</p>' . "\n";
            }
        else // data contains problems!
            {
            echo '<p>Please enter a valid IP address.</p>' . "\n";
            }
        }
    else // Form submission was spoofed!
        {
        echo '<p>An illegal operation was encountered.</p>' . "\n";
        }
    echo '</div>' . "\n";
    }
?>
</body>
</html>

This code was adapted from an example on Daniweb

Share

One Response to ' Ping a Server/Remote Host With PHP '

Subscribe to comments with RSS or TrackBack to ' Ping a Server/Remote Host With PHP '.

  1. VF said,

    on April 26th, 2009 at 1:41 am

    I copied and pasted… and I get these errors (PHP 5.2.9-2):

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in w:\www\ip.php on line 31

    Parse error: parse error in W:\www\ip.php on line 31

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word