Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ob_implicit_flush(), flush(), ob_flush() - not working on remote server

If I load this script in chrome from my local server on XAMPP:

header("Content-Type:text/plain");

set_time_limit(0);

$max = 40;
for ($i = 0; $i < $max; $i++) {
    $response = array(  'server time: ' . date("h:i:s", time()), 'progress' => round($i/$max*100));
    echo json_encode($response);
    ob_flush();
    flush();
    sleep(1);
}
ob_clean();

It works as you would expect, every second the page displays a new response. However, when I upload it to my remote server (running the same version of php), it waits until the entire script finishes before it displays the output. On very long scripts, it updates the output every 30-60 seconds or so.

As the title suggests, I've tried using all the different flush functions, but nothing works. There is likely some difference in the php.ini of my local server and my remote server, but I don't know what.

Please help.

---EDIT---

I've been doing some more testing. I've noticed that exactly it only updates every 4096 bytes, which happens to be what my remote server's php ini value for 'output_buffering' is. However, for some reason, if I change output_buffering to '1' or 'off', nothing changes. It still only updates every 4096 bytes.

I'm testing the 2 identical scripts on different servers on the same browser.

like image 590
hedgehog90 Avatar asked Sep 12 '15 12:09

hedgehog90


1 Answers

I didn't take into account nginx, which has it's own output buffer.

I simply added 'header("X-Accel-Buffering: no");' to the top of the php script and it all works fine now.

like image 132
hedgehog90 Avatar answered Sep 21 '22 15:09

hedgehog90