jump to navigation

Gzipping the html February 11, 2006

Posted by rossoft in CakePHP.

For improving the transfer of your page, you can compress it using gzip. Apache already supports that by a module, but php can do it for you by code if you don't have control of your web server.

Simply, put this somewhere before starting the output:

Micro component (I love little components) that do this:

 * GzipOutput component. 
 * Facilitate sending gz-encoded data to web browsers that support compressed web pages.
 * You can test gzip compression of your site in the url
 * http://www.whatsmyip.org/mod_gzip_test/
 * @author      RosSoft
 * @version     0.3
 * @license		MIT

class GzipOutputComponent extends Object
	function startup(&$controller)
		static $once=true;

//only send gzip if browser supports it

$encodings = explode(',', strtolower(preg_replace('/s+/', '', $encodings)));
		if ( $once && in_array('gzip', $encodings))



1. rossoft - May 8, 2006

Improved version. Checks if browser supports gzip

2. naonak - May 23, 2006

Hi Miguel,

When I do requestaction with this component, I have error :

Warning: ob_start() [ref.outcontrol]: output handler ‘ob_gzhandler’ cannot be used twice in […]\app\controllers\components\gzip_output.php on line 47

How resolve it ?

3. naonak - May 23, 2006

Work with that :
if (ob_get_length () === FALSE && in_array(‘gzip’, $encodings))
register_shutdown_function(‘ob_end_flush’);// Not indispensable but…

4. rossoft - May 23, 2006

Don’t know…the component is called twice ? Are you using requestAction or something like that?

5. naonak - May 23, 2006

Yes it’s because I am using requestAction but I posted a trick to resolve

6. Naonak - May 24, 2006

Don’t work with IE

7. rossoft - May 24, 2006

I have tested it now, and works in IE. It must be your fault

8. Currency - October 25, 2007

Nice blog, Very useful information here, Thanks for sharing.

9. evalle - December 26, 2009

the handler ob_gzhandler already check if the navigator supports gz it a bit redundant, php info

evalle - December 26, 2009

sorry bad this is the correct link http://php.net/manual/en/function.ob-gzhandler.php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: