Reputation: 1635
I want to delay some outputs using sleep()
or usleep()
.
That works great in the terminal, but when I run the script on a server which has gzip enabled, the output will be buffered completely and then gzipped and then sent to the browser.
Is there a way to either force the server (Apache) to not buffer the script,
or to force the server to clear the buffer,
or to find out in PHP wether or not Apache will gzip the stuff?
I could then disable all delays, which would be helpful if I can't force direct output...
EDIT: Sadly, I cannot edit any configuration files, the behaviour should be consistent across many different php setups.
Upvotes: 1
Views: 339
Reputation: 1635
After searching and trying out a bit, I found out several things.
1) There might be gzip compression which is executed via the zlib library. This can be deactivated on runtime:
ini_set('zlib.output_compression', false);
2) There might additionally be gzipping applied via an Apache module. It is not possible to see, wether or not this is going to happen after code execution, but there is a pretty reliable way to break it:
header("Content-Encoding: none");
This is not standard compliant, but it forces Apache to think that the provided content can possibly not be compressed. So it won't jump in.
There might be a lot of other situations (like nginx, or another gzipping extension, and so on), but in most of the cases, this combination of tricks will do the trick:
// disable zlib
ini_set('zlib.output_compression', false);
// Force termination of all instantiated buffers
while (@ob_end_flush());
// prevent apache from gzipping
header("Content-Encoding: none");
// prevent the browsers from showing a cached version before showing the new one
header('Cache-Control: no-cache');
// Start the output to enable buffering
header('Content-Type: text/html; charset=utf-8' );
// Push the beginning of the page to the browser
ob_flush();
flush();
// Do stuff here.
Hope this helps anyone...
Upvotes: 0
Reputation: 28742
EDIT because of the extra info
Okay, then i say:
You can't have reliable automation.
You can do either a header curl to a small php file on same domain(empty.php which echo's "testing") to see if it gets sent with compression before doing the output.
But this adds delay to first byte in your script. Easiest solution would be a config file for your script that allows the webadmin to say: gzip enabled or disabled before rolling out your script to production.
Because gzip can be used in php itself, server output, proxies, etc... And any SEO concious admin has GZIP enabled by default because it gives bonus points at google. So actually I'd suggest ditching the sleeps and use multiple ajax requests instead to fetch the information that takes longer to prepare or use websockets to provide your information if it really needs the wait.
You need to exclude your file from the caching via apache config file.
See: For php flush - how to disable gzip for specific file?
Put this in your 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
if you have files that use get parameters leave the $
sign off the end of getMyFile\.php
Upvotes: 1