【问题标题】:PHP show results while runningPHP 在运行时显示结果
【发布时间】:2012-12-31 22:39:04
【问题描述】:

我试图在 PHP 脚本运行时显示结果,例如.. 一个非常长的循环,我希望它在页面加载时回显结果,我通过这个搜索了很多,但我不能'找不到好的答案,谷歌搜索后我发现有人说使用 ob_flush 从这个 question .. 但它没有工作,以及从 php.ini 启用 implicit_flush ,仍然没有工作
它仅在进程完成时加载,我尝试像这样运行for 循环

ob_start();

for($i=0; $i<500; $i++){
 echo "hm\n";
 ob_flush();
}

ob_end_flush(); 

仍然没有工作..它一次显示它们

我现在的最后一个猜测是,它需要更多的 PHP 配置来启用/禁用某些东西,
或者..它也可能是 apache2 配置?

与此相关的配置设置是什么?需要通过 Apache 或 PHP 配置禁用/启用的设置..

PS :我确信它可以单独使用 PHP 来完成,我在 GoDaddy 主机上看到它并在几个网站上看到它,其中 http://www.checker.freeproxy.ru/checker/index.php .. 如果你尝试提交会正常显示结果不使用ajax,网站使用PHP和Apache,这背后有一个神秘的秘密

【问题讨论】:

    标签: php linux apache debian flush


    【解决方案1】:

    如上所述,Ajax 将是最好的方法。

    您将需要 3 个文件,一个 html 文件或 php 文件负责这项工作,一个包含您的 ajax 的 javascript 文件和运行您的脚本的 php 文件,这里有一个示例说明如何执行此操作。如果你需要它来调整你想要做的任何事情,其余的取决于你,但如果你相应地分解你的 php,它应该给出一个顺序的 redout。

    go.hml:

    <html>
    <head>
    <title>Insert Title Here</title>
    <script src="ajax_example.js" language="javascript"></script>
    </head>
    
    <body>
    <form action="javascript:insert()" method="post">
      <input type="text" name="limit" value="" id="limit"/>
      <input type="submit" name="Submit" value="Go"/>
    </form>
    
    <div id="text_response"></div>
    </body>
    </html>
    

    ajax_example.js:

    // make script work for internet explorer too
    function createObject(){
      var request_type;
      var browser = navigator.appName;
      if(browser == 'Microsoft Internet Explorer'){
        request_type = new ActiveXObject('Microsoft.XMLHTTP');
      }else{
        request_type = new XMLHttpRequest();
      }
      return request_type;
    }
    var http = createObject();
    
    var response = '';
    var current  = 0;
    var limit    = 0;
    
    function insert(){
      current = 0;
      // write to the document
      response = 'Hang on...';
      document.getElementById('text_response').innerHTML = response;
      // set the limit and run the loop script
      limit = encodeURI(document.getElementById('limit').value);
      limit++;
      loop_file(current);
    }
    
    function loop_file(i) {
      // open the php file you wish to run, the 'hm' and 'rand' are optional, obviously
      http.open('get', 'file.php?hm='+i+'&rand='+Math.random());
      // run the insertReply function
      http.onreadystatechange = insertReply;
      http.send(null);
    }
    
    function insertReply(){
      if(http.readyState == 4){
        response = response+'<br />'+http.responseText;
        document.getElementById('text_response').innerHTML = response;
        current++;
        // this runs like a pseudo for loop and will loop until it reaches the 'limit'
        if(current < limit){
          loop_file(current);
        }else if(current == limit){
          //create end script here
        }
      }
    }
    

    文件.php

    <?php
    echo isset($_GET['hm']) ? $_GET['hm'] . " - hm\n" : "hm\n";
    ?>
    

    【讨论】:

      【解决方案2】:

      我从这个answer使用这种方式

      while(1) {
        echo "should display these lines on browser while in infinite loop.<br>";
        flush();
      }
      

      或使用for 循环,它们都可以正常工作,为了更准确,我使用ob_flush() 和flush()

      for($i=0; $i<5000; $i++) {
           echo "should display these lines on browser while in infinite loop.<br>";
           usleep(30000);
           ob_flush();
           flush();
      }
      

      它们都可以正常工作没有 ajax

      【讨论】:

        【解决方案3】:

        你不能用 PHP 做到这一点。 PHP 在服务器端运行,因此它在 HTTP 响应发送回浏览器之前执行。

        您需要使用 AJAX 来实现这一点。

        你也可以看看 websockets 来实现这种东西。

        您也可以作弊并将所有数据加载到隐藏列表中,然后在页面加载后使用javascript将列表项一一显示。 :)

        【讨论】:

        • 我记得在 GoDaddy 主机上做过,效果很好,但是你能展示那个 ajax 示例吗?
        • 而且这个网站也符合我的意思:checker.freeproxy.ru/checker/index.php
        • 代理阻止了我:(虽然他们可能正在使用 AJAX
        • 不,我没有看到任何 ajax 进程.. 我试图通过源加载它,它一直等到整个过程完成,但是当我从页面本身执行它时,它在运行时显示结果,您可以从任何代理网站(如 hidemyass.com 或其他网站)看到它,我相信它可能
        • 我添加了另一个选项,你想要这个的原因将决定它是否可行。
        【解决方案4】:

        在这里查看我的帖子:Show progress bar in php while loop

        它还有一些示例代码,几乎涵盖了您需要的所有内容。

        PS:单独使用 PHP 无法完成,需要使用 AJAX + PHP(客户端 + 服务器端编码)来完成。这是因为只有在文件被完全解释后才会将响应发送到浏览器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-11
          • 1970-01-01
          • 2021-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多