Well, here is another tutorial about Gzip. If you haven't already read the tutorial on using mod_gzip to gzip your css and javascript files, take a look at it. This one is about GZipping your PHP output (text/html, text/xml, text/plain, or whatever).
This is very simple to do, and should not require much work on your part. The best way is to use PHP's zlib.output_compression directive. This can not be set via ini_set() because for zlib to compress output, it needs to be initialized before the PHP script starts. This can be done via .htaccess or httpd.conf. If using .htaccess "AllowOverride All" needs to be enabled for the directory containing .htaccess otherwise it will not work.
Add this to (or create) .htaccess
php_flag sets a "flag", A boolean value. 1 is true, 0 is false. So "php_flag zlib.output_compression 1" just tells php to use zlib for output compression.
php_value sets a value of a setting. For zlib.output_compression_level the valid range is -1 to 9. -1 is default, 0 is no compression, 1 is minimal compression, and 9 is maximum compression. It is best to use 1 rather than a higher number like 9. The difference is in compression is not much but the difference in CPU usage is a bit higher, and for most of us, bandwidth is a little more disposable than CPU usage.
An alternative method is to use output buffers in PHP. This does not require "AllowOverride All" apache configuration so it is alot more usable. To use output compression with output buffers you simply start the output buffer with a call back of ob_gzhandler, PHP's built in output handler for gzipping content.
The above code has to be at the beginning of your PHP file(s) BEFORE any other output. Using output buffers is good anyways because it allows you to set headers anywhere in your code and never get "headers already sent" error from PHP.
I know its a very simple tip, but it is definitely a big bandwidth saver and I know its hard to believe but ALOT of people still are not gzipping their output even though it is so simple! Hope this helps.
This is very simple to do, and should not require much work on your part. The best way is to use PHP's zlib.output_compression directive. This can not be set via ini_set() because for zlib to compress output, it needs to be initialized before the PHP script starts. This can be done via .htaccess or httpd.conf. If using .htaccess "AllowOverride All" needs to be enabled for the directory containing .htaccess otherwise it will not work.
Add this to (or create) .htaccess
Apache config / .htaccess Code:
php_flag zlib.output_compression 1 php_value zlib.output_compression_level 1
An explanation:
php_flag sets a "flag", A boolean value. 1 is true, 0 is false. So "php_flag zlib.output_compression 1" just tells php to use zlib for output compression.
php_value sets a value of a setting. For zlib.output_compression_level the valid range is -1 to 9. -1 is default, 0 is no compression, 1 is minimal compression, and 9 is maximum compression. It is best to use 1 rather than a higher number like 9. The difference is in compression is not much but the difference in CPU usage is a bit higher, and for most of us, bandwidth is a little more disposable than CPU usage.
An alternative method is to use output buffers in PHP. This does not require "AllowOverride All" apache configuration so it is alot more usable. To use output compression with output buffers you simply start the output buffer with a call back of ob_gzhandler, PHP's built in output handler for gzipping content.
PHP Code:
<?php // this starts the output buffer. // if zlib.output_compression is set already, it does not use ob_gzhandler as a callback. // ob_gzhandler is not compatible with zlib.output_compression or it double-gzips the output rendering it useless, hence the check. ob_start( ini_get('zlib.output_compression')? 'ob_gzhandler' : '' ); ?>
The above code has to be at the beginning of your PHP file(s) BEFORE any other output. Using output buffers is good anyways because it allows you to set headers anywhere in your code and never get "headers already sent" error from PHP.
I know its a very simple tip, but it is definitely a big bandwidth saver and I know its hard to believe but ALOT of people still are not gzipping their output even though it is so simple! Hope this helps.
discuss this topic to forum
