Reputation: 309
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;
}
Upvotes: 0
Views: 2241
Reputation: 88647
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.
Upvotes: 1