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

i-CONICA used Ask the Experts™

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

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?

// 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 {
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    return $status;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
        Domain name:
          <tr><td><input name="domainname" type="text" /></td></tr>
          <tr><td><input type="submit" name="submitBtn" value="Ping domain"/></td></tr>
// 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>";
        echo "<tr><td style='padding:10px;background:#fcc;border:1px solid #c33;'>http://$domainbase is DOWN</td><tr>";
    echo '</table>';

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Web Expert
I think you should make AJAX call at some interval of time which will execute the above your code.

Hope this helps,


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...
There's a few ways you could attack this. Probably the cleaner way would be to use AJAX, but the simplest would be to make javascript post the form. You'll have to return the domain back into the text field for this to keep working though. Also You should probably put some regex check on the value of domain name for a valid domain as well.

<script type="text/javascript"><!--
  submitForm = function()
    if ( document.domain.domainname.value != '' )

  setTimeout( 'submitForm()', 30000 );

Open in new window

I too agree with Addy though, AJAX would be much cleaner. You could set up multiple domains to check at the same time as well. Basically think of it as a command center list of all the domains you own or manage that you want to make sure they are up (kind of redundant but meh), you could have javascript loop through all domains passing the domain to a script in the backend, and pulling the data back into the page without the page ever refreshing.

You might be right about it being overkill for this example though.


Thanks guys. I agree the AJAX way would be better, but the simple JS will do for this. Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial