Posted By

mb2o on 03/31/11


Tagged

page caching


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

gecube
mb2o
scilec


Simple PHP Page Caching Technique


 / Published in: PHP
 

URL: http://wesbos.com/simple-php-page-caching-technique/

  1. <?php
  2. // define the path and name of cached file
  3. $cachefile = 'cached-files/'.date('M-d-Y').'.php';
  4. // define how long we want to keep the file in seconds. I set mine to 5 hours.
  5. $cachetime = 18000;
  6. // Check if the cached file is still fresh. If it is, serve it up and exit.
  7. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
  8. include($cachefile);
  9. }
  10. // if there is either no file OR the file to too old, render the page and capture the HTML.
  11. ?>
  12. <html>
  13. output all your html here.
  14. </html>
  15. <?php
  16. // We're done! Save the cached content to a file
  17. $fp = fopen($cachefile, 'w');
  18. fclose($fp);
  19. // finally send browser output
  20. ?>

Report this snippet  

You need to login to post a comment.