Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Flush() not working in Chrome

I stumbled upon this function which promised to work across IE, FF & Chrome. But it does not work in Chrome. Is there a work around?

function buffer_flush(){

    echo str_pad('', 512);
    echo '<!-- -->';

    if(ob_get_length()){

        @ob_flush();
        @flush();
        @ob_end_flush();

    }

    @ob_start();
}
like image 373
Kavin Anbazhagan Avatar asked May 14 '11 11:05

Kavin Anbazhagan


People also ask

What is output buffer in PHP?

Output buffering is a mechanism for controlling how much output data (excluding headers and cookies) PHP should keep internally before pushing that data to the client. If your application's output exceeds this setting, PHP will send that data in chunks of roughly the size you specify.

How to use flush in PHP?

The flush() function requests the server to send its currently buffered output to the browser. The server configuration may not always allow this to happen.


1 Answers

There are several components that can have impact on this issue.

Read carefully through the documentation of this function: http://www.php.net/manual/en/function.flush.php

One solution I had was using Apache2 with mod-php (not as fcgi but as native apache-module) and Chromium. The result came immediately and the script was still running and sending more results.

After typing the following two code-lines every echo-command will immediately push the text to the whatever-PHP-backend:

ob_implicit_flush(1);
@ob_end_flush(); // set an end to the php-output-buffer!

But this php backend can have its own buffer. For example I run nginx as webserver and php is used by the fast-cgi module. Nginx itself has its own buffer ... and so on.

The Browser also can buffer the request. But as I experience Chromium (or Google Chrome) has a verry small or no buffer.

Please read the documentation of every function I mentioned to understand what they really do - but specially the documentation of flush().

Personal hint: Do not put extra characters into the output-buffer but read and understand the configuration of your server.

EDIT: If you have gzip enabled the whole response from the server will be buffered.

like image 177
SimonSimCity Avatar answered Sep 20 '22 12:09

SimonSimCity