Most of you may not know about g-zip compression and its benefits. G-ZIP compression can help bring down web page size including your CSS/ Javascript to 5 to 50%. Most of the time you will see your page size going down to nearly 15%.
Why should I use it?
When should I not use it?
How do I do it in php?
Include this is all your php pages.
<?php
// Include this function on your pages
function gzipped_page() {
global $HTTP_ACCEPT_ENCODING;
if( headers_sent() ){
$encoding = false;
}elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
$encoding = 'x-gzip';
}elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
$encoding = 'gzip';
}else{
$encoding = false;
}
if( $encoding ){
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
}else{
ob_end_flush();
exit();
}
}
// At the beginning of each page call these two functions
ob_start();ob_implicit_flush(0);
// Do your main stuff here
echo 'I love compression';
// Call this function to output everything as gzipped content.
gzipped_page();?>
How do I do it in ASP.NET?
Paste this in your web.config, tested for ASP.NET 4.0, 3.5 and should work for 2.0.
IE version 4 and above
Netscape Navigator version 4.06 and above
Mozilla support since at least 1998
FireFox from the very first version.
Opera version 5.12 and above.
Safari had reports of buggy support only in early versions (2003)
Chrome from the very first version
JAWS support began around 2005
K-Meleon support since at least 2002, version 0.6
Lynx support since version 2.6
Camino support at least since version 0.6
Why should I use it?
- You will save bandwidth and a lot of server bandwidth as the size of the content responded is very small. On an average you will save about 80% of your bandwidth.
- You will bring down your load times by 80-90%. That is one thing that matters. Having a low load time is highly desirable and your users will love that your site is fast.
When should I not use it?
- If you have limited CPU resources. Compression takes some CPU resources so if you have fairly limited resources, compression can be taxing. This is rare though. The CPU or the server needs to be very old for this case.
- If you server very large dynamic web pages above 400KB. Compressing large dynamic content can be expensive.
How do I do it in php?
Include this is all your php pages.
<?php
// Include this function on your pages
function gzipped_page() {
global $HTTP_ACCEPT_ENCODING;
if( headers_sent() ){
$encoding = false;
}elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
$encoding = 'x-gzip';
}elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
$encoding = 'gzip';
}else{
$encoding = false;
}
if( $encoding ){
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
}else{
ob_end_flush();
exit();
}
}
// At the beginning of each page call these two functions
ob_start();ob_implicit_flush(0);
// Do your main stuff here
echo 'I love compression';
// Call this function to output everything as gzipped content.
gzipped_page();?>
How do I do it in ASP.NET?
Paste this in your web.config, tested for ASP.NET 4.0, 3.5 and should work for 2.0.
<system.webServer> <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"> <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/> <dynamicTypes> <add mimeType="text/*" enabled="true"/> <add mimeType="message/*" enabled="true"/> <add mimeType="application/javascript" enabled="true"/> <add mimeType="*/*" enabled="false"/> </dynamicTypes> <staticTypes> <add mimeType="text/*" enabled="true"/> <add mimeType="message/*" enabled="true"/> <add mimeType="application/javascript" enabled="true"/> <add mimeType="*/*" enabled="false"/> </staticTypes> </httpCompression> <urlCompression doStaticCompression="true" doDynamicCompression="true"/> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer>
Do not worry about compatibility. G-ZIP is supported by all modern browsers.
Here is a list of supported browsers.