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 ?

1
  • 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) Commented Aug 1, 2012 at 22:55

2 Answers 2

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.

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

3 Comments

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').)
Read my answer, it specifically says this doesn't work with ob_start. Then read the documentation which specifically says "This means you will have to call both ob_flush() and flush() to flush the ob output buffers".
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 !
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.