You are invited to Log in or Register a free Frihost Account!

How To: Easily Save Your Bandwidth Using GZIP

I was doing a bit of page design the other day and wondered exactly what the GZip compression often used on Invision and PHPBB was. A quick Google or two and some testing later and I found that:
1.) It is incredibly easy to do.
2.) Client filesize reductions are quite dramatic.

I therefore recommend that anyone who is building a site using PHP do this, there's no reason not to!

All you need to do is use two built-in PHP functions; ob_start() and ob_flush(). The only task is to put them at the start and end of every page you use.

If you're like me then you have a header.php (for DocType, <head> etc) and footer.php (for copyright, disclaimer, </html> etc) file anyway which are included on each page. If this is the case then you can simply add
<?php ob_start('ob_gzhandler'); ?>

<?php ob_flush(); ?>

to the start and end of each respectively and hey presto that's it!

If you don't use files like this then its not quite as simple... but still straightforward. So, if you don't use header.php or footer.php files, create some and insert the code as above. Now, either you can rewrite all the files to include these (booooring!) or use a rather nifty .htaccess trick...

Create a file called .htaccess (or open it for editting if you already have one) in any directory where files are directly accessible by the users browser containing the following;
php_value auto_prepend_file /
php_value auto_append_file /

Note that you'll have to change the "/" part to wherever the files actually live. If you get #500 errors when trying to view your files after this, it's probably because you've got the path wrong.

And that's it! Your pages should now be sent to the browser in a compressed form. The size reduction will vary from page to page but just testing it I had a page of 1.42KB reduced down to 0.83KB - that's nearly less than 60% of the original file size.

Further note: You must have obstart() before everything else *including sessions* and obflush() after everything else.
On a side note, after analyzing a packet trace I noticed Google also uses gzip compression.

Thanks for the tip, i've been wondering how it works.
Already using Gzip... not sure how much it improves load times by but I tell you one thing :p it certainly doesnt hurt.
AftershockVibe wrote:
2.) Client filesize reductions are quite dramatic.

You can say that again. Mine went from an average of 23.66kB to 6.56kB Shocked
The average loadtime went from an average of 0.455 to 0.158 ms

I never realised gzip was this good. I'd heard of it but wasn't too sure of what it was and didn't have the time to look into it. Glad you did though Very Happy
I suggest everyone looks into this. Saves bandwidth so you'll be happier and reduces size and speed for your visitors so their happier Wink
mathiaus wrote:

You can say that again. Mine went from an average of 23.66kB to 6.56kB Shocked
The average loadtime went from an average of 0.455 to 0.158 ms

He, yes, the document's size is smaller, but images and other non-php files are still the same, don't forget it.
Then, yes, load runs faster, but page generation is slower, and if server if overflowed, then it will run really slower !
True, but I would have thought it was worth the trade-off. I get bandwidth exceeded problems much more often that I've ever had server load problems.
I'm gonna try this on a few of my web apps. does it add a lot more to the server load to compress each page before it's sent? and how does it affect AJAX applications?
Hate to blow my own threads trumpet but would this be worth sticky-ing? Especially as it is in FriHost's interest to save bandwidth...
It works fine with AJAX infact I think AJAX uses GZIP itself.. I know XAJAX does atleast.
Related topics
Avatar Size
"We Hate Aliens" Campaign [ENDED]
Saving bandwidth when hosting phpbb forums...
Do you code HTML or do you use wysiwyg editors?
Validation help needed
How to write a code which will keep bandwidth to a minimum?
Can I copy/upload entire directories in a frihost account
ASCII Art Generator
more then just the one hosting plan
Building a Website Offline
Weird IMG problem?
Trying to start out!!!
Halo 3 Legendary Map Pack Hands-on
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.