Created
June 25, 2013 18:27
-
-
Save paulund/5861002 to your computer and use it in GitHub Desktop.
Here is a quick code snippet which will allow you to easily cache pages in PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//cache file | |
$cachefile = 'cached/'.date('M-d-Y').'.php'; | |
//Total time the file will be cached in seconds set to 10 hours | |
$cachetime = 36000; | |
//If the cache file already exists and the cache file is over 10hours old then display the cache file | |
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { | |
include($cachefile); | |
exit; | |
} | |
//Start the buffer | |
ob_start(); | |
?> | |
<html> | |
outputs everything in the html tag | |
</html> | |
<?php | |
//Opens cache file | |
$fp = fopen($cachefile, 'w'); | |
fwrite($fp, ob_get_contents()); | |
fclose($fp); | |
ob_end_flush(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment