PHP DIY – How to Add Caching to ANY website!

by max on Sunday, January 4th, 2009

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!

Rate

1 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

ad ad
ad ad


20,000 GPS POI's $9.99

Related News and Resources

Other Interesting News From Our Friends

  • Bit like the RSS post above this, another useful DIY tip that I'm going to use.
  • Very informative article, which I found quite useful. Cheers ,Jay
blog comments powered by Disqus
If you like this post then please subscribe to my full feed RSS.

You can also subscribe by E-mail by filling out your name and E-mail below:

Name: Email:


Got a new hack, DIY, howto, or gadget? Tip us here.

Try Goohack to find a new Hack:


Featured Sites From Zedomax Blog Network