服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

php ob_start() ob_end_flush()缓存技术简单应用

时间:2016-05-10 17:37来源: 作者: 点击:
本文章介绍了一个简单的关于php入门篇-缓存技术简单应用,有需要的朋友可以看看,这里是利用了ob_start(); ob_end_flush(); 来实例的,代码如下: ?php //definethepathandnameofcachedfile $cachefile = cached

本文章介绍了一个简单的关于php入门篇-缓存技术简单应用,有需要的朋友可以看看,这里是利用了ob_start(); ob_end_flush(); 来实例的,代码如下:

  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. exit;  
  10. }  
  11. // if there is either no file OR the file to too old, render the page and capture the HTML. //开源代码最模板zuimoban.com 
  12. ob_start();  
  13. ?>  
  14. <html>  
  15. output all your html here.  
  16. </html>  
  17. <?php  
  18. // We're done! Save the cached content to a file  
  19. $fp = fopen($cachefile'w');  
  20. fwrite($fp, ob_get_contents());  
  21. fclose($fp);  
  22. // finally send browser output  
  23. ob_end_flush();  
  24. ?>
  25.  
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------