4

I have this code:

   set_time_limit(0);
   header("Cache-Control: no-cache, must-revalidate");
   header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
   ob_flush();
   flush();
   $start = time();
   $secs = time() - $start;
   while ($secs <= 300)
   {         
    echo "this script has been running for $secs seconds.\n";
        ob_flush();
        flush();
        sleep(1);
   }

What I'd like to do when I view this page, is to see in real time how long the script has been running, like this:

  • Script has been running 1 seconds.
  • Script has been running 2 seconds.
  • ............
  • Script has been running 300 seconds.

Instead what I get is a blank window with a continuous 'loading' sign for 5 minutes, and after 5 mins suddenly i'm bombarded with a load of these messages which i should've been getting 1 message at a time.

Can someone explain what I'm doing wrong?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    Have you read all the possible problems listed in the [`flush` documentation](http://www.php.net/manual/en/function.flush.php)? – Artefacto Nov 16 '10 at 00:34

1 Answers1

3

ob_flush is not flush. ob_flush clears the object buffer that's been opened. Since you don't have an object buffer open, nothing is flushed.

Also, web browsers and web server software are notorious for holding up data until it can be outputted. Make sure GZIPing is turned off and that you're using a sane browser.

mattbasta
  • 13,492
  • 9
  • 47
  • 68
  • 2
    He's using both `flush` and `ob_flush`. `ob_flush` may actually be necessary even though he's not using `ob_start` because he may have activate output buffering (or compression) in php.ini. – Artefacto Nov 16 '10 at 00:44
  • To know: `flush` tells the SAPI to flush the output, while `ob_flush` flushes the top-most output buffer on the stack. – Artefacto Nov 16 '10 at 00:46