How to correctly show output at every echo on all browsers?
Asked Answered
A

4

7

I moved my files to a new server and I had a script that instantly showed output on every echo to the browser, but this isn't working on the new server. Here is my test code:

@ini_set('output_buffering', 0); @ini_set('implicit_flush', 1);

for ($i = 0; $i < ob_get_level(); $i++) ob_end_flush();

ob_implicit_flush(1);

ignore_user_abort(true); set_time_limit(0);


$max_wait_time = 30;

$begin_time = microtime(true);

$elapsed_time = 0;


while(!connection_aborted()) {

    echo $i++.str_repeat(' ', 1020).'<br/>';

    flush(); ob_flush();

    usleep(1000000);

    if($elapsed_time > $max_wait_time){ break; }

    $elapsed_time++;

}

I've tried a few things which has become the above. But turning output buffering on and flushing hasn't worked for me. I have tested this on Chrome and Firefox, they both just output everything at the end.

Any ideas?

Augean answered 16/3, 2011 at 12:4 Comment(0)
A
13

Excerpt from the flush documentation:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. [...]

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.

Chances are high that you changed to a different web server (or web server configuration), which buffers the output of the whole script before outputting it.

Arterial answered 20/3, 2011 at 21:1 Comment(1)
This is the best answer you are likely to get. Unfortunately, as the flush() documentation says, you cannot guarantee [from within PHP] that there is nothing else buffering the data. Messing around with Apache settings might help, if that's possible for you, but otherwise I would suggest aiming for an alternate solution.Dendrochronology
F
1

The setting you're looking for is in your PHP.ini and it's called output_buffering:

; output_buffering
;   Default Value: Off
;   Development Value: 4096
;   Production Value: 4096

Set it to off manually and restart your webserver to make flush() actually flush something when you want it, not after 4kb of data :)

Note that ini_set doesn't always necessarily has to work for this. If you want full control, disable it in php.ini itself, or as a .htacces php_value flag

Funk answered 23/3, 2011 at 18:13 Comment(0)
L
0

Browsers decide for themselves when to output content. So if you don't meet that threshold, they'll just wait until it is met and only then show more content to the user.

Lungi answered 20/3, 2011 at 13:0 Comment(3)
That is true, but I don't think that is the case for me since this worked just fine when using a different host as soon as I've put it on Media Temple this started happening. So I don't think its a browser issue.Augean
You mean servers, right? It's up to browsers on when they'll display the content, but they'll suck in anything the servers send, when it's sent.Phalansterian
They suck it in, but that doesn't mean they'll show it right away. Or so I was told...Lungi
U
0

Try to add to .htaccess

    php_value output_buffering Off
Unhesitating answered 26/3, 2011 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.