Building an HTML Cache of your Database Driven Site

If you're just wanting to cache a single PHP page, this is really all you need. However if you have an article.php script and want to cache all your articles what you'll want to do is pull from your database your complete list of articles, and then for each article run through the system I outlined above. You just need to edit the get_include_contents function to pass your article id, or whatever you use to query your database, as well as the filename.

You might also need to make some changes to your files if a single file is responsible for generating multiple pages such as article.php would be. For instance any functions declared in article.php would end up being declared over and over again as you build all your article pages, so you'll want to move those function declarations out into the base caching control file. You might also need to modify server side file paths if any are used in your files.

If you have a search engine friendly URL setup you will also need to build actual directories for use with your new HTML files. If you only have a few categories you can do this by hand, but if you have many such virtual categories you'll want to do it automatically. To do this you'll want to query your database and pull whatever information you need for your category names, and then build your directories using the following PHP code.

mkdir("/full/path/to/your/root/$categoryname", 0755);

If you use a search engine friendly URL setup and also treat your article ids as directories you will want to generate directories at multiple levels as needed and then for your actual HTML page generate it as an index.html page in the category corresponding to it's article id.

This should be all the code you need, with a few tweaks depending on your setup, for you to start caching your heavily dynamic pages as static HTML. It doesn't take as long to run as you might think either, it only took me a few minutes to cache around 1500 very database heavy pages.