Page 1 of 1

Detecting webserver compression

Posted: Tue Feb 20, 2007 4:40 pm
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) ?

Posted: Tue Feb 20, 2007 4:52 pm
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.

Posted: Tue Feb 20, 2007 5:15 pm
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)); 
	}

Posted: Tue Feb 20, 2007 8:43 pm
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.