PHP DIY – How to Add Caching to ANY website!

In my recent ventures, I have resorted to finding great PHP code to cache my website.

Even if you are not using WordPress, you can still make a simple PHP caching mechanism without the use of WordPress Super Cache.  (Well, all WordPress Super Cache is a PHP cache with user-friendly menus.)

Here’s how to add caching to your website:

Copy and paste the following code and save as “cache.php” in your website root directory:

<?php
// Settings
$cachedir = ‘cache/’; // Directory to cache files in (keep outside web root)
$cachetime = 600; // Seconds to cache files for
$cacheext = ‘cache’; // Extension to give cached files (usually cache, htm, txt)

// Ignore List
$ignore_list = array(
‘addedbytes.com/rss.php’,
‘addedbytes.com/search/’
);

// Script
$page = ‘http://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]; // Requested page
$cachefile = $cachedir . md5($page) . ‘.’ . $cacheext; // Cache file to either load or create

$ignore_page = false;
for ($i = 0; $i < count($ignore_list); $i++) {
$ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
}

$cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
@clearstatcache();

// Show file from cache if still valid
if (time() – $cachetime < $cachefile_created) {

//ob_start(‘ob_gzhandler’);
@readfile($cachefile);
//ob_end_flush();
exit();

}

// If we’re still here, we need to generate a cache file

ob_start();

?>

Then copy and paste the following and save as “endcache.php“:

<?php

// Now the script has run, generate a new cache file
$fp = @fopen($cachefile, ‘w’);

// save the contents of output buffer to the file
@fwrite($fp, ob_get_contents());
@fclose($fp);

ob_end_flush();

?>

In you .htaccess file, add:

php_value auto_prepend_file  /home/httpd/vhosts/stattt.com/httpdocs/cache.php
php_value auto_append_file  /home/httpd/vhosts/stattt.com/httpdocs/endcache.php

You can always comment these lines out if you want to turn off caching.

For deleting cache, you can copy and paste the following as save as “clear.php“:

<?php

// Settings
$cachedir = ‘cache/’; // Directory to cache files in (keep outside web root)

if ($handle = @opendir($cachedir)) {
while (false !== ($file = @readdir($handle))) {
if ($file != ‘.’ and $file != ‘..’) {
echo $file . ‘ deleted.<br>’;
@unlink($cachedir . ‘/’ . $file);
}
}
@closedir($handle);
}

?>

Now, make sure your cache directory is CHMODded to 777 and you should start caching your website. 🙂

Also, you can schedule a CRON job for the clear.php to clear cache once a day or how often you’d like to.

This caching worked like a charm for Stattt.com, a stats website coded from scratch.

I did find this from Addedbytes.com, thanks to the author!

3 Responses to PHP DIY – How to Add Caching to ANY website!

  1. Pingback: PHP DIY - How to Add RSS Feeds to Any Website/Blog using Magpie RSS Parser! | zedomax.com - The DIY, HOW TO, Hacks, Gadgets, and Tech Blog/Search Engine!

  2. Bit like the RSS post above this, another useful DIY tip that I’m going to use.

  3. Directory says:

    Very informative article, which I found quite useful. Cheers ,Jay

Leave a Reply

Your email address will not be published.




Related News and Resources