Detecting webserver compression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Detecting webserver compression

Post by WaldoMonster »

If the php zlib is enabled can be detected with ini_get()

Code: Select all

if (ini_get('zlib.output_compression') == true)
Is there a way to detect if the web server has compression enabled (like deflate, gzip) ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't recall the web server needing to have compression enabled to use PHP's own solution.

Typically all I'd do is call ob_start() with ob_gzhandler() for the argument.
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

Here a better description what I want to realize.
When creating a PDF file I want to check if I must send the Content-Length: or not.
Something like this, but then with web server deflate and gzip detection:

Code: Select all

if (ini_get('zlib.output_compression') == false) 
	{
	// Content_Length is not correct when GZIP is enabled
	header('Content-Length: ' . strlen($buffer)); 
	}
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Code: Select all

if(extension_loaded("zlib") && !get_cfg_var('zlib.output_compression') && (strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip"))
{
	// your output
}
This is a cut down version from the gzip output filter in my Template Lite package. It will check to see if the library is loaded and if the server has it enabled by default. You don't want it to compress twice if the server has auto compression enabled. Plus, the last check is to see if the browser supports gzip.
Post Reply