Jump to content
  • Advertisement
Sign in to follow this  
n14Charlie

[web] PHP flush

This topic is 3483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys I need help using the PHP flush function. I'm making a one time call to a php script and I want it to keep outputing stuff back to the client before it ends (inside a loop) I tried using the flush function but it doesn't work.. can anyone help? C.

Share this post


Link to post
Share on other sites
Advertisement
The default behavior in PHP is that whenever you output something, it is directly sent to the user browser (which is often a good idea because it uses less server-side memory).

If you wish to disable this default behavior, you have to use output buffering. You do this by calling the function ob_start(): any text you output after calling that function is kept on the server in a special buffer, and remains there until you flush or clear the buffer. The buffer is automatically flushed when the script ends.

Share this post


Link to post
Share on other sites
As ToohrVyk mentioned, PHP by default echos the output immediately without any buffering so no flush should be required. If you (or some script you are using) has turned output buffering on, you might want to use ob_flush() or ob_end_flush() to turn it off completely. See PHP Output Control. Try putting the following lines before your loop starts:

ob_implicit_flush(true);
ob_end_flush();


If this makes any difference, then the problem was with PHP's output buffering.

Also remember that sometimes web servers and web browsers also perform their own buffering, this is something you cannot easily control via PHP.

If you are using Apache you might want to read this comment that shows how to disable all kinds of buffering and gzip compression at the start of the script. And to quote more from that page:

Quote:
Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the </table> tag of the outermost table is seen.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.


Share this post


Link to post
Share on other sites
I've been reading a lot about performance lately, and output buffering is one of the best solutions, because it allocates big blocks of memory at a time. After you call flush, you need to call ob_start() again to restart buffering.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!