马上注册,结交更多好友,享用更多功能,让你轻松玩转南昌530论坛
您需要 登录 才可以下载或查看,没有账号?注册会员
×
- ob_start() //开启缓存
-
- ob_get_contents() //获取缓存到的内容
复制代码- <?php
- //查找有没有缓存文件的存在
- if(file_exists('./cache/index.html')){
- //有缓存文件直接调用
- include './cache/index.html';
- //获取当前时间戳
- $now_time = time();
- //获取缓存文件时间戳
- $last_time = filemtime('./cache/index.html');
- //如果缓存文件生成超过指定的时间直接删除文件
- if(($now_time - $last_time)/60>30){
- unlink('./cache/index.html');
- }
- exit;
- }
- //开启缓存
- ob_start();
-
- ?>
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE-Edge" />
- <title>南昌论坛</title>
- <meta name="copyrihgt" content="www.nc530.com"/>
- <link rel="shortcut icon" href="./favicon.jpg" type="image/x-icon"/>
- <link rel="stylesheet" href="./static/css/style.css" />
- </head>
- <body>
-
- ...省略html或php代码...
-
- </body>
- </html>
- <?php
- //在文件代码末尾获取上面生成的缓存内容
- $content = ob_get_contents();
- //写入到缓存内容到指定的文件夹
- $fp = fopen('./cache/index.html','w');
- fwrite($fp,$content);
- fclose($fp);
- ob_flush(); //从PHP内存中释放出来缓存(取出数据)
- flush(); //把释放的数据发送到浏览器显示
- ob_end_clean(); //清空缓冲区的内容并关闭这个缓冲区
- ?>
复制代码
|