MacNN Forums (http://forums.macnn.com/)
-   Developer Center (http://forums.macnn.com/developer-center/)
-   -   Compressing HTML output with PHP (http://forums.macnn.com/79/developer-center/276604/compressing-html-output-with-php/)

 
moodymonster Nov 24, 2005 09: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:

http://uk.php.net/manual/en/function.ob-gzhandler.php

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 07:04 AM
 
Love Calm Quiet Nov 25, 2005 10:21 PM
VERY nice tip. Gracias!
 
- - e r i k - - Nov 26, 2005 11: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 10: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 01:02 PM
Code:

//
// 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') )
                {
                        ob_start('ob_gzhandler');
                }
        }
        else if ( $phpver > '4.0' )
        {
                if ( strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip') )
                {
                        if ( extension_loaded('zlib') )
                        {
                                $do_gzip_compress = TRUE;
                                ob_start();
                                ob_implicit_flush(0);

                                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:

http://moodymonster.com/mgfs/index.php

the main index file is ~45K

the compressed version:

http://www.mobilegamefaqs.com/index.php

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:

ob_start("ob_gzhandler");

right at the top of the code, resulting file:

http://moodymonster.com/mgfs/index_gz.php

file size: ~9K
 
All times are GMT -4. The time now is 12:27 PM.

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


Content Relevant URLs by vBSEO 3.3.2