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

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.

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");
?>

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.

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

Related

Is it possible to make a single PHP script ignore FastCGI's output buffer?

I have a PHP script that needs to process some more information in the background after returning the response.
I made it work by following this answer as well as turning off FastCGI Output Buffer:
<IfModule mod_fcgid.c>
FcgidOutputBufferSize 0
</IfModule>
It won't work without turning off FastCGI Output Buffer.
However, I only have one script that needs this. It would be nice if I can keep FastCGI Output Buffer for all other scripts.
Is it possible to just make one PHP script ignore FastCGI Output Buffer?
Maybe you can use nested Apache directives to check the request URI.
Like this:
<IfModule mod_fcgid.c>
<If "%{REQUEST_URI} == '/path/to/script.php'">
FcgidOutputBufferSize 0
</If>
</IfModule>

IE 9 hangs when server replies with a huge response

I am testing download functionality for which I have written a php script. This script reads a large file (10MB) using fopen and fread. Using fread I am reading a chunk of 10MB in a buffer and that buffer I am echoing and then flushing out using flush and ob_flush functions. So that client receives this buffer and I am able to check number of bytes received. This works fine in Chrome, FireFox and IE 8 but when I tested it in IE 9, the browser hangs and I am not able to get the proper downloaded bytes.
Can someone suggest what are the other options that we can use to test this?
define('CHUNK_SIZE', 1024*10240);
$handle=fopen("10MBFile", 'rb');
$buffer=fread($handle, CHUNK_SIZE);
echo $buffer;
ob_flush();
flush();
Regds
I had numerous strange hangups with IE9+ and apache and could fix it with adding this stuff into httpd.conf:
# Deal with user agents that deliberately violate open standards
#
<IfModule setenvif_module>
BrowserMatch "MSIE 10.0;" bad_DNT
</IfModule>
<IfModule headers_module>
RequestHeader unset DNT env=bad_DNT
</IfModule>
AcceptFilter https none
AcceptFilter http none
EnableSendfile Off
EnableMMAP off

Stop Apache Blocking ETags

I have written a web application that uses someone else's API that requires ETags. I have tried this code:
header("ETagbleh: whatever");
Which works perfectly. However, when I set this:
header("ETag: whatever");
Nothing happens. I have heard that it may be Apache blocking the sending of ETags, but I'm not sure. I've done a search for ETag in my apache2.conf and can't find anything to uncomment / remove, so I came here to ask.
So, how can I stop Apache blocking my headers?
Edit: I'm using Apache 2.2.22, and I assumed that the scripting language was irrelevant, given that PHP 5.4.4, which is what I'm using, can set any other header fine.
I've had the same problem.
A very popular way to remove ETags in Apache2 is adding the following configuration:
Header unset ETag
FileETag None
Remove the first config line, if you find it in your configuration.
A bit more difficult to find is mod_include causing the problem. By default the ETag-Header is removed by this module. But you can allow it by configuration. So add something like this:
<IfModule mod_include.c>
SSIETag on
</IfModule>
See here for more information.

In script PHP output buffer settings ignored by server

I have been trying to flush the output of certain scripts to the browser on demand, but they do not work on our productions server.
For instance, I tried running the "Phoca Changing Collation tool" (find it on Google) and I don't see any output until the script finishes executing.
I've tried immediately flushing the buffer on other script that works fine on any server but this one using the following code:
echo "something";
ob_flush();
flush();
Setting "ob_implicit_flush(1);" doesn't help either.
The server is Apache 2.2.21 with PHP 5.2.17 running on Linux. You can see our php.ini file here if that will help:
http://www.smallfiles.org/download/1123/php.ini.html
This isn't the only problem we are having with the server ignoring in-script directives. The server also ignores timeout coding such as:
ini_set('max_execution_time', 900*60);
AND
set_time_limit(86400);
Script always times out at the php.ini default.
Doesn't seem to matter if script is executed in IE or Firefox.
Solved this mystery. Both of them.
To fix the output buffer problem, I needed to turn off gzip compression inside the .htaccess file, though I wish I could just do it in-script.
Here's what you should put in your .htaccess file:
<IfModule mod_gzip.c>
mod_gzip_on No
</IfModule>
SetEnv no-gzip dont-vary
To fix the script terminating without error, I checked my Apache log files and found it wasn't PHP but in the Apache configuration:
The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed
Had to increase the Apache timeout to prevent this error from making it look like my scripts were timing out. Enabling KeepAlive in Apache also helped to resolve the issue once and for all.
Hope this helps someone and thanks for everyone elses' help!
You could be loading an incorrect php.ini file, as it tends to change based on directory.
To check your loaded php.ini file, write: echo php_ini_loaded_file();, which will give you the directory it's in. see php.net
if that fails, see serverfault.com for the server stackexchange site.

how to use gzip for a css/js file in PHP?

Now I'm not making use of any compression method,just put the below between the body tag:
<link rel="stylesheet" type="text/css" href="Client/Css/all.css" />
Is it possible to use ob_start("ob_gzhandler") in PHP to compress this css file?
And how?
this is not generally done programmatically, it is handled by the web server
For apache, there's a mod_gzip or mod_deflate module that you can use.
IIS has settings that you can use.
Yes you could (when piping the files through a php script that performs the compression). But there is much more to it than simply compressing the output. You should only send compressed output to clients that support it, you should make sure you properly handle caching information/requests. Etc.
In quite some cases your webserver contains support for doing all that for you without the need of a PHP script. In case of Apache look at mod_deflate and mod_mime (directive AddEncoding).
There are several ways.
You could tell the webserver to treat the file as PHP (renaming to have a .php extension would be the simplest) and then add:
<?php
header("Content-type: text/css; charset=utf-8");
ob_start("ob_gzhandler")
?>
to the top.
You could write a PHP script that does the same thing, but reads in the CSS file instead of having it inline.
Both this options lead to caching issues - you would have to take care of the cache control HTTP headers too if you want to be sane about it.
The best option would be to forget about PHP and just configure your webserver to compress those files (using mod_deflate, mod_gzip or similar)
If you are using a newer version of the apache web server add the following to your .htaccess file:
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
What are you trying to accomplish? If you're trying to shrink components to send to the clients browser so that they're downloaded more quickly, you should be using mod_defalte. If that's not an option, you can find more here: first result from googling "gzip php css"
EDIT: I just want to point out that unless you're using an older version of Apache, you should use mod_deflate not mod_gzip.

Categories