A miracle called gzip
Posted on 28/5/06 by Felix Geisendörfer
Update: I just found a little issue with this technique inside CakePHP. Read more about this here: Issues with output buffering in CakePHP
Well, beeing on a 64 kbit connection (that's a long story ...) I've beeing bothered more then once by how long this blog took to load. So having played with gzip compression on other websites of mine like fg-webdesign.de in recent times, today I thought, why not do it with thinkingphp.org?
The results are quite astounding. The html of the initial start site was 125 kbyte before. Now putting in gzip compression it's just tiny 14 kbyte, 11,2% of the initial size! I think this is one of these 5 minute fixes that are worth more then hours of coding in certain projects ... ; ). Btw. the reason for the initial Html code to be that big is Geshi which outputs a lot of Html and CSS for syntax highlighting my PHP code I publish here.
Here is the code I just put on top of my header.php for my wordpress theme: (Nothing exiting, so)
header('Content-type: text/html; charset: UTF-8');
$offset = -1;
$ExpStr = "Expires: " .
gmdate('D, d M Y H:i:s',
time() + $offset) . ' GMT';
The page should now load around 9-10x as fast as it has before ; ).
You can skip to the end and add a comment.
Hm what are the advantages on those? I just read this method will check if the browser has (says he has) gzip support and only use gzip in those cases - seemed like a good choice to me.
This might not be an issue for you, since it looks like you're disabling browser caching, but there's a bug in IE6 where it will read cached (uncompressed) files at their gzipped size, causing it to truncate the uncompressed version of the file.
Have you tested on IE at all? If so, what has been your experience?
Hi Nate: I've tested it under IE 6 and it seems to work without any problems. But since I'm not working with caching, I don't know if that would be an issue - I might test it soon.
I also have a custom .htaccess in my cakephp webroot folder on fg-webdesign.de where I run my CSS / JS through gzip compression. I know Cake has a feature for that too, but it didn't work well for me (having to change path's, etc.) so I made my own solution (which has like 7-8 lines of code).
Btw. when working with CakePHP app's you can put the code from above inside your beforeFiter() off AppController so all Html will be gziped ; ).
I might do some more research on this topic combined with another little tutorial since I think making websites load fast is *very* important ; ).
[...] Output buffering is a useful way to accomblish things in php like gzipping all html output and such. A while ago I wrote about it on this blog (see "A miracle called gzip") as well. [...]
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
An alternative is to use mod_deflate or mod_gzip.