Basic website compression and caching with PHP

March 1, 2013 by Asbra — Leave a comment

Why would I want to compress and/or cache my website?
Compressing your web content saves bandwidth, and bandwidth can be expensive if you run a big website with a lot of traffic and conserving bandwidth means that your website will load quicker for visitors.
Caching your web pages saves your servers resources, and you really don’t want to run out of resources while you have traffic on your websites, which could make the server crawl to a halt, losing potential sales.

This is where GZIP comes into the picture. GZip is now the most popular website compression format. I will show you how you can use GZip to compress your PHP-pages, aswell as how to cache them to make them load up quicker, in a way that even avoids PHP from loading up at all.

We will be using output buffering in PHP to be able to do caching. What is output buffering? When enabled, we store the entire output from PHP into a buffer that we can write to a file, before displaying it to the visitor. Or not displaying it at all, if you would like that.

To get started, add this somewhere at the start of your page. Everything that comes after it will be put into the output buffer.


Now to display the content of the output buffer to a visitor, we call ob_end_flush() function.


What we want to do before we flush the buffer is to save the buffer into a file. This file is the cached version of this web page, then later when another visitor asks the web server to see the same page, we will show him the cached page instead of having PHP creating a new page. This way we save alot of resource by avoiding to run PHP at all.
So, let’s save the contents of the buffer to a file.

$cached = fopen( "cache/$file.html", 'w' ); // replace $file with your page request URI
fwrite( $cached, ob_get_contents() );       // write the output buffer (the web page) to the cache-file
fclose( $cached );                          // close the file handle
ob_end_flush();                             // flush the output buffer (display the content to the visitor)

In case you are wondering about the $file variable; for the sake of this example it should be the request URI, if your page is accessed by http://mywebsite.tld/page-one then your request URI is page-one

Now finally, pop this into your .htaccess file (this assumes you have mod_rewrite installed & enabled, if unsure, just try it)

RewriteCond %{DOCUMENT_ROOT}/cache/$1.html -f
RewriteRule ^(.*) %{DOCUMENT_ROOT}/cache/$1.html [L]

This will make your webserver look for a cache-file with the name of the request, if you would go to http://mywebserver.tld/page-one it would look for cache/page-one.html, if that file exists it will be served to the visitor instead of triggering the normal script.

To test if GZip compression is enabled and working on your server, there are simple online tools that run tests for you. Here are a couple:

Just fill out your website’s URL and run the test, it will show you the result 🙂
Testing your website compression for gzip

Did this do any difference in load speed of your website? You can test your websites loadspeed and see what to optimize using’s Full Page Test
For a client of mine it did a bit of difference, here are the “before and after” images:
Test of a page without caching
Test of a page with caching
GZip was enabled both before and after the test, the difference here was in the caching. It might not seem much, but it does make a difference when you have a lot of traffic 🙂


Posts Facebook

Blogging out of many years of experience with gamehacking, programming, reverse-engineering and general tomfoolery.

No Comments

Be the first to start the conversation.

Leave a Reply