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


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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Avinash ZalaWeb ExpertCommented:
I think you should make AJAX call at some interval of time which will execute the above your code.

Hope this helps,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
i-CONICAAuthor Commented:
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.
i-CONICAAuthor Commented:
Thanks guys. I agree the AJAX way would be better, but the simple JS will do for this. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.