Link to home
Start Free TrialLog in
Avatar of i-CONICA
i-CONICAFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to repeat this script, using POST data every 30 seconds or so...?

Hi,

I've got this script attached, which checks if a server is responding or not, like "ping example.com"...

I want to modify this script so that every 30 seconds, it'll repeat, to continually recheck the domain.

I thought about using sleep(), but that'd time the script out eventually, and I think using a meta refresh or javascript refresh wouldn't resubmit the post data...

Any thoughts?

Thanks.
<?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 style='padding:10px;background:#cfc;border:1px solid #3c3;'>http://$domainbase is UP ($status ms)</td><tr>";
    else
        echo "<tr><td style='padding:10px;background:#fcc;border:1px solid #c33;'>http://$domainbase is DOWN</td><tr>";
    echo '</table>';
}
?>
</body>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Avinash Zala
Avinash Zala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of i-CONICA

ASKER

Whoa there horsey, I think AJAX is a bit heavy for this? Is there no simpler way? it's just a little tool for me and a couple of the guys here to use now and again when servers go down... :(

If you think that's the only way, it seems a bit convoluted, so might just keep hitting F5...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys. I agree the AJAX way would be better, but the simple JS will do for this. Thanks.