Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For php flush - how to disable gzip for specific file?

I have a ajax call to somefile.php . i want the php script to do a simple task and than send back data to the user, and only than do very time consuming tasks. so i need to flush the output after the first simple task. it doesn't work, probably because i have gzip enables.

I definitely don't want to disable gzip across all the vhost, and also not in all the folder where somefile.php is. i just want to disable it for this specific file. is that possible?

EDIT:

this is what i've included in my apache conf:

<FilesMatch \.php$>
    SetEnv no-gzip 1
</FilesMatch>

this is my php script:

<?php
$sucesss = @apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);

ob_start();

for($i=0;$i<10;$i++)
{
    echo 'printing...';
    ob_flush();
    flush();

    sleep(1);
}
?>

it doesn't work. i still see all the output together after 10 seconds.

like image 484
Moshe Shaham Avatar asked Apr 22 '12 10:04

Moshe Shaham


2 Answers

I was looking for a solutions for the same issue. This is what worked for me but unfortunately it seams NOT to be a VALID header.

<?
header("Content-Encoding: none");
?>
like image 89
B. Martin Avatar answered Oct 04 '22 17:10

B. Martin


apache_setenv() is correct. See the documentation.

http://php.net/manual/en/function.apache-setenv.php#60530

apache_setenv('no-gzip', '1');

Your problem is that you turned on output buffering with ob_start(). Comment that out.

I've learned that apache_setenv() is only available with the PHP Apache module. It's not available when using FPM. In that case, you have to use .htaccess to turn off GZip. An example is

https://stackoverflow.com/a/36212238/148844

RewriteRule ^dashboard/index - [E=no-gzip:1]
SetEnvIf REDIRECT_no-gzip 1 no-gzip

The - means NOOP, E means set variable, 1 is the value. After redirects, the variables are renamed and prepended with REDIRECT_.

If the output is still being buffered, check if you are going through a proxy or cache. See if headers like Via: 1.1 varnish or Via: 1.1 vegur are present. They will buffer the response also.

like image 34
Chloe Avatar answered Oct 04 '22 17:10

Chloe