Help Editing PHP Ping Script

YoCrunch

New member
May 25, 2010
84
1
0
S.Fla
I'm still a complete noob when it comes to php and I'm trying to learn how to submit several websites for checking as opposed to one.

How would I go about making it so I could paste a list of websites to be checked instead of one website at a time with this script?

*The script isn't mine, just a random script found on the internet*

PHP:
<?php    
    // Check whether the for was submitted
    if (isset($_POST['submitBtn'])){
        $domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
        $domainbase = str_replace("http://","",strtolower($domainbase));
        
        echo '<table>';

        $status = pingDomain($domainbase);
        if ($status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>";
        else               echo "<tr><td>http://$domainbase is DOWN</td><tr>";

         echo '</table>';
    }
?> 

And we are all done. 

The complete code looks like this:

<?php
// Function to check response time
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
        Domain name:
        <table>
          <tr><td><input name="domainname" type="text" ></td></tr>
          <tr><td><input type="submit" name="submitBtn" value="Ping domain"></td></tr>
        </table>  
      </form>
<?php    
    // Check whether the for was submitted
    if (isset($_POST['submitBtn'])){
        $domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
        $domainbase = str_replace("http://","",strtolower($domainbase));
        
        echo '<table>';

        $status = pingDomain($domainbase);
        if ($status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>";
        else               echo "<tr><td>http://$domainbase is DOWN</td><tr>";

         echo '</table>';
    }
?>
</body>
 


change your form input to a textarea instead of the single line form input, then use the explode() function with the operator being "\n" (new line). That will output an array, that you can loop through and do your pinging with.

Use delays when you ping.
 
change your form input to a textarea instead of the single line form input, then use the explode() function with the operator being "\n" (new line). That will output an array, that you can loop through and do your pinging with.

Use delays when you ping.

Thanks a lot man, much appreciated