Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Compressing HTML output with PHP

Compressing HTML output with PHP
Thread Tools
Mac Elite
Join Date: Sep 2003
Location: London
Status: Offline
Reply With Quote
Nov 24, 2005, 09:54 PM
 
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.
     
Mac Elite
Join Date: Sep 2003
Location: London
Status: Offline
Reply With Quote
Nov 25, 2005, 07:04 AM
 
     
Mac Elite
Join Date: Mar 2001
Location: CO
Status: Offline
Reply With Quote
Nov 25, 2005, 10:21 PM
 
VERY nice tip. Gracias!
TOMBSTONE: "He's trashed his last preferences"
     
Posting Junkie
Join Date: May 2001
Location: Brisbane, Australia
Status: Offline
Reply With Quote
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?

[ fb ] [ flickr ] [] [scl] [ last ] [ plaxo ]
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Nov 27, 2005, 10:35 AM
 
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.
     
Mac Elite
Join Date: Sep 2003
Location: London
Status: Offline
Reply With Quote
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
( Last edited by moodymonster; Nov 27, 2005 at 01:15 PM. )
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -4. The time now is 11:35 AM.
All contents of these forums © 1995-2015 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2015, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2