MacNN Forums (
-   Developer Center (
-   -   Compressing HTML output with PHP (

moodymonster Nov 24, 2005 08:54 PM
Compressing HTML output with PHP
A site I work recently had its hosting upgraded and pages that were previously 45K (ish) are now between 3K and 8K. This made me curious as to why. Upon inspection I found that you can compress HTML pages on the fly.

This is new to me so thought I'd share:

one line of code, and ta da 80% smaller file size. If the browser can't handle it, it just supplies the normal page.

v cool - If you're feeling brave you could modify the ini file to do the zlib thing. Being as the site isn't hosted on my mac I haven't done that 'cause I like to see how large the html output is.
moodymonster Nov 25, 2005 06:04 AM
Love Calm Quiet Nov 25, 2005 09:21 PM
VERY nice tip. Gracias!
- - e r i k - - Nov 26, 2005 10:25 AM
Is this supposed to be added before the doctype even? And how can we test if it actually is working?
jersey Nov 27, 2005 09:35 AM
Quote, Originally Posted by - - e r i k - -
I...And how can we test if it actually is working?
you can test it here. I use it all the time. If you are with a hosting company they should have gzip or mod_deflate on by default (if of course, they are running apache). If they dont, I'd consider switching hosts.

Compression (mod_deflate on Apache 2.0.54 runing off of a g4 sawtooth under my desk) knocked my pages down bt 74%. Awesome.
moodymonster Nov 27, 2005 12:02 PM

// gzip_compression
$do_gzip_compress = FALSE;
if ( $board_config['gzip_compress'] )
        $phpver = phpversion();

        $useragent = (isset($_SERVER["HTTP_USER_AGENT"]) ) ? $_SERVER["HTTP_USER_AGENT"] : $HTTP_USER_AGENT;

        if ( $phpver >= '4.0.4pl1' && ( strstr($useragent,'compatible') || strstr($useragent,'Gecko') ) )
                if ( extension_loaded('zlib') )
        else if ( $phpver > '4.0' )
                if ( strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip') )
                        if ( extension_loaded('zlib') )
                                $do_gzip_compress = TRUE;

                                header('Content-Encoding: gzip');

^ found this is PHPbb files (includes/page_header.php). The code goes right at the top before any output. Although that code is for PHPbb and can't just be stuck into something else.

However as other people have pointed out, its better to get the server software to do it.

To test if its working: access the page in Safari without the compression on and view the activity window - you can get the file sizes. Then turn compression off and try it. If it is working you should see a marked decrease in the file size for the main html document.

If you look here:

the main index file is ~45K

the compressed version:

index file ~9K

Thats not using the PHP method though, its running through the server software but the principal is the same.

Though I'd try it using the PHP method by pasting:


right at the top of the code, resulting file:

file size: ~9K
All times are GMT -4. The time now is 10:54 AM.

Copyright © 2005-2007 MacNN. All rights reserved.
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.

Content Relevant URLs by vBSEO 3.3.2