Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling output buffering in PHP

I have an object for tasks and on __deconstruct(), it's meant to run some of the lengthier cleanup tasks after the rest of the page has already loaded. Unfortunately, it buffers the output and won't send it until after the tasks are finished (nothing is printed in the tasks).

I've read through http://www.php.net/flush and tried all the suggestions there. Obviously I've tried disabling output_buffering in php.ini. I've disabled deflate_module, zlib compression is off, don't have mod_gzip. Calling flush() or ob_flush() has no effect, nor does enabling implicit_flush.

I'm just running XAMPP (currently apache 2.2.17, php 5.3.4) under Windows Server 2008 R2. PHP is being run as a module.

And yes, I could set up some little AJAX hack to run the task manager or even set up a scheduled task to run this specific task, but output buffering has been an issue elsewhere, too. Would just like it to be gone sometimes.

From a similar thread, someone suggested seeing what the following would do:

<?php
while (TRUE)
{
    echo 'x';
    flush();
    sleep(1);
}
?>

As expected, the page displays nothing until the maximum execution time is reached, at which point it flushes the buffer.

This has become extremely frustrating. Anyone have any ideas what could still be causing it to buffer?

like image 741
Apropos Avatar asked Mar 15 '12 20:03

Apropos


1 Answers

You're only sending a small amount of data. Browsers have their own buffer, which can be based on a number of bytes, by which elements have been received, or by something else.

In short, there is nothing you can do about this. The buffering is happening client-side, not server-side. You could try sending more data before your xs.

You can prove this by packet sniffing the connection between the server and the browser, with Wireshark or similar.

like image 161
Brad Avatar answered Sep 22 '22 10:09

Brad