6

I'm trying to display the results while PHP script is running, as example.. a really long loop, i want it to echo the results while the page is loading, I searched really a lot through this and i couldn't find the good answer, After googling i found people saying use ob_flush from this question .. but it didn't work, as well as enabling the implicit_flush from php.ini , still didn't work
it only loads when the process is finished, i tried to run a for loop like this

ob_start();

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

ob_end_flush(); 

and still, didn't work.. it shows them all at once

My last guess now is that it needs more PHP configurations to enable/disable some stuff,
or.. it could also be apache2 configurations ?

What are the config settings that are related to this ? settings that needs to be disabled/enabled through Apache or PHP configurations ..

P.S. : I'm sure its possible to be done using PHP alone, I saw it done on GoDaddy hosting and saw it on several websites, of them http://www.checker.freeproxy.ru/checker/index.php .. if you try to submit it will show the results normally without using ajax, the website uses PHP and Apache, there's a mystery secret behind this

Community
  • 1
  • 1
Osa
  • 1,922
  • 7
  • 30
  • 51

4 Answers4

4

I used this way from this answer

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

or using the for loop, they both work fine, and it to make it more accurate i use ob_flush() with flush()

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

they both work fine without ajax

Community
  • 1
  • 1
Osa
  • 1,922
  • 7
  • 30
  • 51
2

You can't do this with PHP. PHP is run server side so it executes before the HTTP response is sent back to the browser.

You would need to use AJAX to achieve this.

You may also look at websockets to achieve this kind of thing.

You could also cheat and load all the data into a hidden list, and then use javascript to show the list items one by one after the page has loaded. :)

cowls
  • 24,013
  • 8
  • 48
  • 78
  • I remember doing it on GoDaddy host and it worked fine, however can you show that ajax example ? – Osa Jan 17 '13 at 11:42
  • And also this website does what i am saying : http://www.checker.freeproxy.ru/checker/index.php – Osa Jan 17 '13 at 11:46
  • Thats blocked by proxy for me :( they could be using AJAX though – cowls Jan 17 '13 at 11:49
  • no, i don't see any ajax process.. i tried to load it via the source and it waited till the whole process is complete,, but when i do it from the page itself, it shows results while its running, you can see it from any proxy website like hidemyass.com or something, i'm sure its possible – Osa Jan 17 '13 at 11:51
  • I added one other option, the reason you want this will determine if its viable. – cowls Jan 17 '13 at 11:55
  • i just added an answer of what i was on about, check it out – Osa Jan 17 '13 at 12:16
  • Could be interesting, ill check it out – cowls Jan 17 '13 at 12:19
2

Check my post here: Show progress bar in php while loop

It has some sample code as well, and covers pretty much everything you need.

PS: It can't be done with PHP alone, you need to do this with AJAX + PHP (client + server side coding). This is because the response is sent to the browser only after the file is fully interpreted.

Community
  • 1
  • 1
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
2

As mentioned above, Ajax would be the best method.

You'll need 3 files, a html file or php file that heads the job, a javascript file with your ajax in it and the php file running your script, here's an example of how you could do this. The rest is up to you if you need it tweaking for whatever you are trying to do, but it should give a sequential redout if you break up your php accordingly.

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";
?>
Lucas
  • 1,476
  • 13
  • 20