Need some help with this code

Discussion in 'PHP' started by bluemouse2, Dec 13, 2008.

  1. #1
    This code is for pinging a website. Can you please modify it to ping an array of websites from a database?

    
    <?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>  
    PHP:
     
    bluemouse2, Dec 13, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You would do something like this:

    
    
    <?php    
        // Check whether the for was submitted
            
            echo '<table>';
    
            $query = mysql_query("SELECT domain_url FROM my_domain_table...");
            
            while($domain_array = mysql_fetch_assoc($query)):
    
            $status = pingDomain($domain_array['domain_url']);
    
            if ($status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>";
            else               echo "<tr><td>http://$domainbase is DOWN</td><tr>";
    
            endwhile;
             echo '</table>';
    
    ?>
    
    
    PHP:
     
    jestep, Dec 13, 2008 IP
  3. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Thank you. Problem is solved now.
     
    bluemouse2, Dec 13, 2008 IP