Jump to content

php website alive status script help needed please


boemboem

Recommended Posts

Hello all,

 

I've found a script wich can ping and echo a server status, the code is below:

<?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>';
    }
?>

 

What I want is that I can use this script for give me the outcome of more servers. So I changed the form method to "get" to see the url wich is used. for example this:

http://www.mydomain.com/checkwebsitestatus.php?domainname=www.google.nl&submitBtn=Ping+domain

 

When I put this into the adresbar it does not give my a outcome.

 

Can somebody tell me how to fix this script so I can display multiple domains beneath eachothr like this for example

 

* http://www.google.nl is ALIVE (26 ms)

* http://www.yahoo.com is ALIVE (133 ms)

* http://www.klinkklareonzintoch.nl is DOWN

 

I hope somebody understands what I mean an can help me also.

Link to comment
Share on other sites

<?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" >
              <input name="domainname[]" type="text" >
              <input name="domainname[]" type="text" >
              <input name="domainname[]" type="text" >
              <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']) && is_array($_POST['domainname'])){
      echo '<table>';
      foreach($_POST['domainname'] as $domainbase){
        if(empty($domainbase)) continue;
        $domainbase = str_replace("http://","",strtolower($domainbase));
        $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>';
    }
?>

Link to comment
Share on other sites

<?php

//Fill this in with the list of servers
$servers = array(
  'www.google.nl',
  'www.yahoo.com',
  'www.klinkklareonzintoch.nl',
);

// 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>
  <table>
<?php    
  foreach($_POST['domainname'] as $domainbase){
    $status = pingDomain($domainbase);
    if ($status != -1)
      echo "<tr><td>$domainbase is ALIVE ($status ms)</td><tr>";
    else
      echo "<tr><td>$domainbase is DOWN</td><tr>";
  }
?>
</table>
</body>
</html>

Link to comment
Share on other sites

oops...forgot to change that variable:

 

<?php

//Fill this in with the list of servers
$servers = array(
  'www.google.nl',
  'www.yahoo.com',
  'www.klinkklareonzintoch.nl',
);

// 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>
  <table>
<?php    
  foreach($servers as $domainbase){
    $status = pingDomain($domainbase);
    if ($status != -1)
      echo "<tr><td>$domainbase is ALIVE ($status ms)</td><tr>";
    else
      echo "<tr><td>$domainbase is DOWN</td><tr>";
  }
?>
</table>
</body>
</html>

Link to comment
Share on other sites

ok, now I have this :)

 

foreach($servers as $domainbase){

 

it seems to work, but goves me also this:

Is there a way to exclude the warnings?? I think it's part of the fsockopen function, but I really hope it can be fixed, because it looks like s**t on a website :)

 

line 13:

$file      = fsockopen ($domain, 80, $errno, $errstr, 10);

 

 

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/g35003/coldcharlie.nl/HTML/beta1/multi.php on line 13

 

Warning: fsockopen() [function.fsockopen]: unable to connect to www.klinkklareonzintoch.nl:80 (Unknown error) in /var/www/g35003/coldcharlie.nl/HTML/beta1/multi.php on line 13

 

www.google.nl is ALIVE (27 ms)

www.yahoo.com is ALIVE (125 ms)

www.klinkklareonzintoch.nl is DOWN

 

 

[edit]ahh, it was the same idea :)[/edit]

Link to comment
Share on other sites

The errors would be because the sites do not exist, but that is what the function should find out so you can suppress the errors by using an @ symbol in front of the fsockopen();

 

Like so

 

@fsockopen ($domain, 80, $errno, $errstr, 10);

 

what he said

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.