0

Possible Duplicate:
what is output buffering?

I have some content that takes a while to be generated by PHP. Meanwhile I would like to use this simple JS trick to display a "Loading..." message. The message would disappear as soon as the output is generated.

<p id="loading_msg">Loading...</p>

<h1>Hello</h1>
<?php
    sleep(2); // This is the greedy function call
    print '<p>This content definitely took a while to be generated !.</p>';
?>

<script type="text/javascript">
    var e = document.getElementById("loading_msg");
    e.style.display = "none";
</script>

The problem is, instead of sending everything up to sleep(2); then blocking, it will wait before all the page is generated, and then, send it all at once. It obviously breaks the purpose of my "Loading..." message, because it doesn't appear before the 2 seconds have elapsed. Any ideas of why, and how I could work around this ?

Community
  • 1
  • 1
mdup
  • 7,889
  • 3
  • 32
  • 34
  • Question has beeb closed as a duplicate of one about output_buffering - however this q is about getting content to render in the client before the server has finished (implying the opposite of output buffering) – symcbean Aug 01 '12 at 22:55

2 Answers2

4

By default, PHP will buffer your output and send a fewer number of larger chunks to the user. You can use flush to send the contents of write buffer to the user immediately:

<p id="loading_msg">Loading...</p>

<h1>Hello</h1>
<?php
    flush();
    sleep(2);

This won't work if you've previously enabled output buffering by calling ob_start or the php.ini directive output_buffering to 1.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • I already thought of a buffer problem. Unfortunately, I added `ob_start()` at the first line and `flush()`ed before the `sleep()` and it still behaves the same. (Also tried various combinations with other functions like `ob_flush()` or `ini_set('output_buffering','on')`.) – mdup Jul 18 '12 at 15:34
  • 2
    Read my answer, it specifically says this doesn't work with `ob_start`. Then read the [documentation](http://php.net/manual/en/function.flush.php) which specifically says **"This means you will have to call both `ob_flush()` and `flush()` to flush the ob output buffers"**. – user229044 Jul 18 '12 at 15:38
  • Got it, I read a little to fast, sorry. It took me a while to understand how `ob_*` functions relate to each other. It indeed works with both `ob_flush(); flush();`. Thank you ! – mdup Jul 18 '12 at 16:01
1

Using 'flush' might work on some webservers / browsers (meagar is correct that it won't work while output buffering is enabled - but that doesn't mean it will work the rest of the time).

It's up to the webserver to decide when to revert to chunked encoding - most will start a chunked response when you call flush as long as you're not compressing the output stream. Then it's up to the browser to decide when it starts rendering an incomplete response - and behaviour will vary depending on the content too - e.g. none will start rendering a table until it has the closing tag.

A better solution then trying to handle it in the receiving page, is to handle it in the sending page - e.g. using a litebox to display the message. Alternatively the message could be presented in an interim page which does a redirect to the generated content.

symcbean
  • 47,736
  • 6
  • 59
  • 94