我正在使用WOL PHP脚本,我希望找到一种方法来使用ajax刷新部分页面,或者我应该调用外部php脚本吗?
我的PHP脚本启动一台电脑,然后检查多次以查看它是否已启动并运行....但是,使用PHP时,用户显然必须等到它完成所有操作才会显示结果,我我希望有一种方法可以让我在运行的“while循环”运行并在页面的第一部分加载后更新?
本质上,checkstatus函数检查端口是否正在运行,然后显示输出..我希望加载主页面,然后使用ajax或其他php脚本更新portcheck的状态?不确定如何。
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;
}
答案 0 :(得分:0)
发布评论说“你不能在一个PHP的实例中做到这一点”我现在将继续并证明自己是错的。
可能使用某种server push设置执行此操作。请考虑以下代码:
<?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>
但是,我仍然认为最好将循环抽象到客户端并使用AJAX轮询服务器,该服务器一次执行一次检查。