PHP Code

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&#91;'ping_ip_addr'&#93;; 
$ping_count   = $_POST&#91;'ping_count'&#93;; 
 
// 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&#91;'PHP_SELF'&#93;; ?>" 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&#91;'submit'&#93; == 'Ping' ? htmlentities($ping_ip_addr, ENT_QUOTES) : $_SERVER&#91;'REMOTE_ADDR'&#93;;; ?>" 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&#91;'submit'&#93; == '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

1 Comments

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

Your email address will not be published. Required fields are marked *

*
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.
Anti-spam image