PHP在运行时显示结果

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

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

仍然没有工作,它立刻显示出来

我现在最后的猜测是,它需要更多的PHPconfiguration来启用/禁用一些东西,
或..也可能是apache2configuration?

什么是与此有关的configuration设置? 需要通过Apache或PHPconfiguration禁用/启用的设置

PS :我敢肯定它可能只使用PHP完成,我看到它在GoDaddy托pipe完成,并在几个网站上看到它们,其中http://www.checker.freeproxy.ru/checker/index.php ..如果你试图提交它会正常显示结果, 而不使用Ajax ,网站使用PHP和Apache, 这背后有一个神秘的秘密

检查我的帖子在这里: 显示进度条在PHP while循环

它也有一些示例代码,几乎涵盖了你所需要的一切。

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

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

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

你也可以看websockets来实现这种事情。

您也可以将所有数据加载到隐藏列表中,然后使用javascript在页面加载后逐个显示列表项。 🙂

我用这种方式从这个答案

 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

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

你需要3个文件,一个html文件或者php文件,一个带有你的ajax的javascript文件和一个运行你的脚本的php文件,下面是一个如何做到这一点的例子。 其余的由你决定,如果你需要调整你正在做的任何事情,但是如果你相应地分解你的php,它会给你一个连续的重做。

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 } } } 

file.php

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