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?

1
  • 1
    Have you read all the possible problems listed in the flush documentation? Commented Nov 16, 2010 at 0:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
To know: flush tells the SAPI to flush the output, while ob_flush flushes the top-most output buffer on the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.