Speed up your Website with this Simple Page Cache Script for PHP

For dynamic websites, database queries take up a significant amount of the CPU resources of your servers. For medium to high traffic websites  in shared or virtual hosting services, this could mean significant loss of traffic due to too much database queries or over use of shared CPU time. As such, it is quite useful to have some sort of cache system for your dynamic pages, particularly those with content that don’t change too much or too often anyway.

Below is a very simple and generic cache script for PHP web pages which you can copy-paste and use almost instantly in your website. What it does is store an instance of the dynamically generated web page as an HTML file and it then counter checks with a predefined time period that you set whether to display the cached instance or to dynamically generate the page again. The longer the time period between dynamic generation, the greater the savings in terms of CPU usage and database queries.

1. First create a folder in your hosting account where you’ll be housing the cached data. In this example the cache folder is located at http://www.yourdomain.com/cache

2. Next, copy-paste the script below at the start of the PHP page you want cached. We are assuming here that the dynamically generated page is utilizing the GET variable “id”, such as: http://www.yourdomain.com/index.php?id=XXXX:

ob_start(); //turn on output buffering for PHP

//check if variable id is present and is an integer, if not, redirect to homepage
if ($_GET['id']=="" or !is_numeric($_GET['id']))

//absolute path to cache file. For simplicity the cached version of the page is saved as the ID number
$cachefile = $_SERVER['DOCUMENT_ROOT']."/cache/$id.html";
//duration before the cache is refreshed in seconds. Setting below is for a 3 day cache. Change as you see fit.

$cachetime = 259200;

// Serve from the cache if it is younger than $cachetime

if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))

include($cachefile); //display the cache file

// if cache is display, exit the script


//if cache is not present or is too old, run the rest of the script......

//place a time stamp on the cache to easily check when a page was last cached...you can place this a meta variable in your HTML header i.e. <meta name="date" content="<?=$cachedate;?>"/>
$cachedate=date("F d Y H:i:s");


3. At the bottom of your PHP page, copy-paste the code below:

//define the cache file name
$cachefile = "cache/$id.html";
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
// Send the output to the browser

4. You’re done! Upload and update your PHP file.

11 Replies to “Speed up your Website with this Simple Page Cache Script for PHP

  1. Cool, but not efficient with CMS which have frequent data changed.. example is, im updating and updating the contents while the cache is not expired yet.

    1. A simple solution is to ‘refresh’ or delete the cache file whenever an actual update has occurred. Meanwhile you save on database lookups whenever the page is simply visited but not changed.

  2. this is great. worked perfectly! id never implemented caching before and this made it very easy. A lot easier than the other solutions i have googled.

    The only problem was because the code is pasted in as text the quotes are the wrong ones etc but other than that its perfect.

    Thank you!!!

  3. Thanks for the cache script !

    Any plans to update to inlude md5 hash capable and delete cache function.

    The script worked great right out of the box and your installation instructiion were also very good.



  4. Update!!

    This is a cache delete function that I have tested and use with your cache script.

    I added this on the same page just above the cache script and it works well.

    $cachedelete) {
    The most important part of this is the correct and full path to the cache directory.

    Also, when I tried to use md5 encryption the script works but it does not place the file in the cache directory.

    $cachefile = md5(“cache/$file.txt”);

    Instead it places the encrypted file name in the root directory and this won’t work for my application.

    Any thoughts on how to get the script when using md5 to place the file in the cache directory would be of great help.

    Again thanks for publishing your cache script .


  5. Sorry,
    // Amount of time in seconds before file is deleted
    $cachedelete= ‘86400’;
    // full path to cache directory/folder
    $dir = ‘/home/xxxxx/public_html/cache’;
    //full path to cache directory/folder
    if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
    if ($file != “.” && $file != “..”) {
    if ((time() – filemtime($dir.’/’.$file)) > $cachedelete) {

  6. Update;

    I was able to get the md5 encryption to work by assigning a new file name to the md5 encription request like this:

    $file being the original file title to be sent to the cache directory

    $myfile = md5($file.txt)

    $myfile being the md5 encrypted file title to be sent to the cache directory.

    $cachefile = “cache/$myfile”;

    Also I tried to use gzcompress on the $myfile contents to reduce the cache directory size like this:

    fwrite($fp, gzcompress(ob_get_contents(), 6 ));

    and this worked but the contents of the file is written to the file compressed and when $myfile is recalled to the browser I get the gzip contents displayed in the browser which can’t be understood.

    So I need some help with how to use gzcompress to write the contents to the file but when $my file is recalled to the browser it can be read .

    Some how I would need to uncompress the $myfile if it is recalled to the browser so the original contents can be displayed


    Your cache script can be used on both alpha and numeric files by simply removing the redirect request as I have done and the script works well if you have non-numeric file titles to cache.

  7. Hi, I am not using a get variable ID is am using post data through a proxy. can I use the same script?

Comments are closed.