14

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.

Moshe Shaham
  • 15,448
  • 22
  • 74
  • 114
  • 1
    If it's your web server that handles the gzip compression, I think you can't disable it via PHP. – noli Apr 22 '12 at 10:25
  • Maybe look at this : http://stackoverflow.com/questions/1922934/how-to-disable-mod-deflate-in-apache2 – noli Apr 22 '12 at 10:27

3 Answers3

10

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");
?>
B. Martin
  • 1,047
  • 12
  • 18
  • Here is the `Content-Encoding` specifications: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5 – Chloe Mar 23 '16 at 23:28
  • 1
    `header("Content-Encoding: identity");` sounds more correct. – Déjà vu Oct 04 '19 at 03:26
  • I am using SimpleHTMLDom for scraping which does the job and more, but unfortunately it is returning compressed data. I have a fix for that but it doesn't fit in the function that I am using. So I tried these tips to prevent the page from sending Gzip data but none of the suggestions here work. – WilliamK Sep 12 '20 at 02:31
  • @e2-e4 I know that `identity` is correct but sometimes you have proxy servers or other configurations that you cannot change that interfere an encode all traffic. – B. Martin Oct 07 '20 at 16:57
4

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.

Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357
2

Put this in httpd.conf

# exclude certain page requests (e.g. for requesting getMyFile.php?action=getFile&id=3 as non-compressed)
SetEnvIfNoCase Request_URI getMyFile\.php$ no-gzip dont-vary
PadraigD
  • 641
  • 5
  • 13
  • How can you disable it only for certain requests to the same file? e.g.: `ajax.php?a=list&id=5` -> with compression `ajax.php?a=download&file=image.png` -> without compression – B. Martin Feb 03 '14 at 16:06
  • @B.Martin Leave the `$` off of the regex and include the request parameter. Like `ajax\.php\?a=download`. – Chloe Mar 24 '16 at 23:04