0

I have a WOL PHP script I am working on, and I was hoping to find a way to refresh part of the page using ajax, or if I should call an external php script?

My PHP script starts a pc and then checks multiple times to see if it is up and running.... However, with PHP the user obviously has to wait until it is all the way done before it displays the results, I was hoping there was a way to make the "while loop" I am running run and update after the first part of the page has loaded?

Essentially, the checkstatus function checks to see if a port is running, then displays output.. I was hoping to load the main page, then update the status of the portcheck using ajax, or another php script? Not sure how.

function CheckStatus($tries, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $maxtries, $broadcast, $udport, $timeout){
    while($tries <= $maxtries){
        echo "<br \>";
        echo "Try $tries of $maxtries";
        echo "<br \>";
        //echo portcheck($deviceip, $deviceport);
        if (portcheck($deviceip, $deviceport) != 0){
                echo "  ------>  Could NOT open connection to $device at $deviceip on port $deviceport";
                echo "<br \>";                
                //echo "<br /> TRIES: $tries";

            }else{
                echo "<br \>";
                echo "<br \>";
                echo "<br \>";
                echo "TESTED OKAY!  $device";
                return TRUE;
            }
            $tries=$tries + 1;
    }
    echo "<br />";
    echo "MAX TRIES REACHED... COULD NOT CONNECT!";
    return FALSE;
}   
1
  • 1
    You can;t do it in a single instance of PHP, you need to perform your initial operations, output a web page and exit. Then your javascript will poll the server with ajax, which will execute one iteration of the loop per request, and return the result. Essentially you need to abstract the loop to the client side, and just have the server perform a single check at a time. Commented May 15, 2012 at 18:34

1 Answer 1

1

Having posted a comment saying "You can't do it in a single instance of PHP" I will now go ahead and prove myself wrong.

It is possible to do this using a sort of server push setup. Consider the following code:

<?php

  // Do all your preliminary stuff here

?>
<html>
  <head>
    <title>Server push demo</title>
    <script type="text/javascript">
      function updateTries(tries) {
        document.getElementById('num_tries').innerHTML = tries;
      }
      function pushEvent(str) {
        var event = document.createElement('div');
        event.innerHTML = str;
        document.getElementById('stream_container').appendChild(event);
      }
    </script>
  </head>
  <body>
    <div>
      Number of tries:
      <span id="num_tries"></span>
    </div>
    <div id="stream_container"></div>
<?php

  // Push all output so far to the client
  flush();

  // Start status check loop
  CheckStatus($tries, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $maxtries, $broadcast, $udport, $timeout);

  function CheckStatus($tries, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $maxtries, $broadcast, $udport, $timeout){

    while ($tries < $maxtries) {

        echo "<script type='text/javascript'>updateTries($tries);</script>";
        flush();

        if (portcheck($deviceip, $deviceport) != 0){

          echo "<script type='text/javascript'>pushEvent('Could NOT open connection to $device at $deviceip on port $deviceport');</script>";
          flush();

        } else {

          echo "<script type='text/javascript'>pushEvent('TESTED OKAY! $device');</script>";
          flush();
          return TRUE;

        }

        $tries++;

    }

    echo "<script type='text/javascript'>pushEvent('MAX TRIES REACHED... COULD NOT CONNECT!');</script>";
    flush();
    return FALSE;

  }

?>
</body></html>

However, I still maintain it is better to abstract the loop to the client side and use AJAX to poll the server, which performs one check at a time.

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried implementing this solution... but it still operates as before... it will wait to load the page until all the processing is complete... You can see my entire code here: docs.google.com/open?id=0Bz170qNApm2ANHJHRHE2bmVNZ1E Do you see anything I missed? I call the page with this php code here: docs.google.com/open?id=0Bz170qNApm2ANjFNM3BMdGU5Ulk
Ah, nvm, had gzip enabled on server, and for some reason had to add ob_implicit_flush(1); to the top, remove all the flush commands and replace them with echo str_repeat(' ',1024*64); To clear the buffer I guess.... It works now, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.