PHP 任何方式“回显、显示、打印”循环中间的消息?

发布于 2024-12-10 14:48:54 字数 898 浏览 0 评论 0原文

我需要强制我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靑春怀旧 2024-12-17 14:48:54

在刷新函数的手册页中提到了一些需要检查的事项:

flush() 可能无法覆盖 Web 服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲没有影响


一些服务器,尤其是 Win32 上的服务器,仍然会缓冲脚本的输出,直到脚本终止,然后再将结果传输到浏览器。


Apache 的服务器模块(例如 mod_gzip)可能会进行自己的缓冲,这将导致 flash() 不会导致数据立即发送到客户端。

评论中还提到了一些需要检查的配置文件选项

如果flush()函数不起作用。您必须在 php.ini 中设置下一个选项,例如:

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:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser


Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.


Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

There are also some config file options that need to be checked mentioned in the comments

If flush() function does not work. You must set next options in php.ini like:

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.

孤檠 2024-12-17 14:48:54

正如前面所说,除非您完全控制服务器,否则这是不可能实现的,即使您这样做,如果您想在相当长的时间内保持连接打开,它也可能会超时,需要客户端继续。

因此,使用 AJAX 可能是一个更好的主意,例如:

ping.php脚本

$answer = shell_exec('COMMAND');
$r = array();
list($r['src'], $r['app'], $r['cid'], $r['non'], $r['flag'], $r['dur'], $r['exten']) = explode("!", $answer);
if($r['cid'] > 0)
  $r['success'] = true;
else
  $r['success'] = false;
echo json_encode($r);

公共 HTML/PHP 文件中的

<script type="text/javascript">
  // If you're using jQuery...
  var ping = function() {
    $.ajax('ping.php', {
      dataType: 'json',
      success: function(data) {
        if(data.success && confirm('You have a call from ' + data.cid + ': show data>'))
          $('iframe').attr('src', 'new_page.php');
      }
    });
  };
  setInterval(ping, 3000);
</script>

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

$answer = shell_exec('COMMAND');
$r = array();
list($r['src'], $r['app'], $r['cid'], $r['non'], $r['flag'], $r['dur'], $r['exten']) = explode("!", $answer);
if($r['cid'] > 0)
  $r['success'] = true;
else
  $r['success'] = false;
echo json_encode($r);

Script in your public HTML/PHP file

<script type="text/javascript">
  // If you're using jQuery...
  var ping = function() {
    $.ajax('ping.php', {
      dataType: 'json',
      success: function(data) {
        if(data.success && confirm('You have a call from ' + data.cid + ': show data>'))
          $('iframe').attr('src', 'new_page.php');
      }
    });
  };
  setInterval(ping, 3000);
</script>
何时共饮酒 2024-12-17 14:48:54

将其放入 htaccess (gzip 轮流)中:

SetEnv no-gzip dont-vary

并在循环中执行以下操作以回显消息并刷新输出缓冲区:

echo $msg;
ob_flush();
flush();

Drop this into your htaccess (turns of gzip):

SetEnv no-gzip dont-vary

and to echo the message and flush output buffer do this in your loop:

echo $msg;
ob_flush();
flush();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文