PHP 任何方式“回显、显示、打印”循环中间的消息?
我需要强制我的 PHP 页面在循环中的每一轮之后显示消息..而不是在循环结束时..因为我正在运行无限循环。 目标是我在页面内有一个脚本,该脚本在服务器上运行命令.. 获取输出并将数据与 IF 语句中的数据进行比较,如果它检查它显示一条消息.. 如果没有.. 它会休眠三秒钟,然后再次运行循环。 问题是当我从服务器上的命令行运行 php 文件时,它完全执行了它应该执行的操作..每 3 秒显示一条消息(不按确认按钮)..但是当我在浏览器中运行它时,它显示什么都没有(它等待循环结束!) 但不确定我下面的例子是否能达到目的。
<?php
while (1==1) {
$answer = shell_exec ("COMMAND'");
list($src, $app, $cid, $non, $flag, $dur, $exten) = explode("!", $answer);
if ($cid > 0) {
echo "<script language='javascript'>
var r=confirm('You have a call from $cid: Show Data?')
if (r==true)
{
OPEN A WEB PAGE INSIDE AN IFRAME
}
</script>";
//header( 'refresh: 15; url=monit.php' );
flush();
sleep(10);
}
else {
//header( 'refresh: 1; url=monit.php' );
sleep(1);
}
}
?>
请注意,我尝试使用“header()”函数重新加载页面,但我不想重新加载页面本身(会在父页面内创建复杂情况)
有什么建议吗?
I need to force my PHP page to display the message after each round in a loop .. not at the end of loop .. as i'm running an infinite loop.
the goal is that i have a script inside a page that runs a command on the server .. takes the output and compares the data with in IF statement if it checks it displays a message .. if not .. it sleeps for three seconds and runs the loop again.
the problem is when i run the php file from a command line on the server it does exactly what it should do .. displays a message every 3 seconds (not pressing the confirmation button).. but when i run it in a browser it displays nothing (it waits for the loop to end!)
not sure if my below example will serve the purpose though.
<?php
while (1==1) {
$answer = shell_exec ("COMMAND'");
list($src, $app, $cid, $non, $flag, $dur, $exten) = explode("!", $answer);
if ($cid > 0) {
echo "<script language='javascript'>
var r=confirm('You have a call from $cid: Show Data?')
if (r==true)
{
OPEN A WEB PAGE INSIDE AN IFRAME
}
</script>";
//header( 'refresh: 15; url=monit.php' );
flush();
sleep(10);
}
else {
//header( 'refresh: 1; url=monit.php' );
sleep(1);
}
}
?>
Please note that i tried to use "header()" function to reload the page however i don't want to reload the page itself (will create a complication inside the parent page)
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在刷新函数的手册页中提到了一些需要检查的事项:
评论中还提到了一些需要检查的配置文件选项
output_buffering = Off
;输出处理程序=
zlib.output_compression = 关闭
;zlib.output_handler =
我还注意到有注释掉的行会导致页面刷新 - 您无需刷新页面即可查看新输出,浏览器应将页面显示为仍在加载,直到脚本完成。
There are a few things to check mentioned in the manual page for the flush function:
There are also some config file options that need to be checked mentioned in the comments
output_buffering = Off
;output_handler =
zlib.output_compression = Off
;zlib.output_handler =
I've also notice there are commented out lines to cause the page to refresh - you do not need to refresh the page to see the new output, the browser should show the page as still loading until the script has completed.
正如前面所说,除非您完全控制服务器,否则这是不可能实现的,即使您这样做,如果您想在相当长的时间内保持连接打开,它也可能会超时,需要客户端继续。
因此,使用 AJAX 可能是一个更好的主意,例如:
ping.php脚本
公共 HTML/PHP 文件中的
As has been said unless you have full control over the server this would be impossible to implement, and even if you did if you wanted to keep the connection open for a considerable length of time it would probably time out, requiring client-side continuation.
Thus, it would probably be a better idea to use AJAX, for example:
ping.php
Script in your public HTML/PHP file
将其放入 htaccess (gzip 轮流)中:
并在循环中执行以下操作以回显消息并刷新输出缓冲区:
Drop this into your htaccess (turns of gzip):
and to echo the message and flush output buffer do this in your loop: