2

My flush mechanism stopped working, i'm not sure why. I'm trying to run a simple flush example now, with no luck:

echo "before sleep";
flush();
sleep(5);
echo "after sleep";

after doing some reading, and understanding ngin x was was installed on my server lately, I requested it to be disabled for my domain. (the server admin said he disabled it for this specific domain)

also, i tried disabling gzip, added these lines to .htaccess

SetOutputFilter DEFLATE
SetEnv no-gzip dont-vary

also, tried adding these to my php file

ini_set('output_buffering','on');
ini_set('zlib.output_compression', 0);

nothing helps. its sleeping 5 seconds and then displaying all the content together. I've been using it before, and have been using also through the output buffer (ob_start, ob_flush etc., now just trying to make the simplest example work)

normalppl
  • 297
  • 2
  • 12

2 Answers2

0
<?php
ini_set('zlib.output_handler', '');
ini_set('zlib.output_compression', 0);
ini_set('output_handler', '');
ini_set('output_buffering', false); 
ini_set('implicit_flush', true); 
apache_setenv( 'no-gzip', '1' );


for($i = 0; $i < 5; $i++){
    echo str_repeat(chr(0), 4096); #flood apache some null bytes so it feels the packet is big enough to be sent...
    echo "$i<br/>";
    flush();
    sleep(1);
}
?>
Loïc
  • 11,804
  • 1
  • 31
  • 49
0

"Stopped working" is a pretty high level. You should actually take a look what works or not to find out more.

This can be done by monitoring the network traffic. You will see how much of the response is already done and in which encoding it's send.

If the response is getting compressed, most compression functions need a certain number of bytes before they can compress them. So even you do a flush() to signal PHP to flush the output buffer, there still can be a place either within PHP output filtering or the server waiting for more to do the compression. So next to compression done by apache, check if your PHP configuration does compression as well and disable it.

If you don't want to monitor your network traffic, the curl command-line utility is doing a pretty well job to display what's going on as well and it might be easier to use it instead of network monitoring.

curl -Ni --raw URL

Make sure you use the -N switch which will disable buffering by curl so you see your scripts/servers output directly.

Please see the section Inspecting HTTP Compression Problems with Curl in a previous answer of mine that shows some curl commands to look into the output of a request while it's done with compression as well.

curl is able to show you eventually compressed data uncompressed and you can disable compression per request, so regardless of the server or PHP output compression settings, you can test more differentiated.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836