Just as the title says, easy php caching.
Step 1: Create a folder in the root of your website to store the cached files and make sure the folder is writable by php. My folder is named appropriately cache.
Step 2: Start the part of the page you want cached with….
// Start Cache
$cachefile = ‘cache/’.basename($_SERVER['REQUEST_URI']); //change cache to whatever you named your folder
$cachetime = 2880 * 60; // Cache for 48 hours before replacing. Change the 2880 to what ever value you want. The formula is hours * 60
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() – $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "“; //you can comment out this line if you don’t want the cache info to show in your html code
exit;
}
ob_start(); // start the output buffer
//start your code here
Step 3: End the part of the page you want cached with….
//end your code
$fp = fopen($cachefile, ‘w’); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser