页面静态化,以及给静态化页面设置过期时间

代码:

<?php
Header("content-type:text/html;charset=UTF-8");
$gid = $_GET['news_id']+0;//商品id
$goods_statis_file = "goods_file_".$gid.".html";//对应静态页文件
$expr = 10;//静态文件有效期,秒
if(file_exists($goods_statis_file)){
$file_ctime = filectime($goods_statis_file);//文件创建时间
if($file_ctime+$expr > time()){//如果没过期
echo file_get_contents($goods_statis_file);//输出静态文件内容
exit;
}else{//如果已过期
unlink($goods_statis_file);//删除过期的静态页文件
ob_start(); //从数据库读取数据,并赋值给相关变量
//include ("xxx.html");//加载对应的商品详情页模板
echo date('Y-m-d H:i:s', time());
$content = ob_get_contents();//把详情页内容赋值给$content变量
file_put_contents($goods_statis_file,$content);//写入内容到对应静态文件中
ob_end_flush();//输出商品详情页信息
}
}else{
$dsn = "mysql:host=127.0.0.1;dbname=seven_month";
$db = new PDO($dsn, 'root', 'root', array(PDO::ATTR_PERSISTENT =>true));
$db->query(" set names utf8 ");
set_time_limit(0);
$sql = "SELECT * FROM `news` ; ";
$data = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
//开启缓存
ob_start();
include ('show.php');
//从数据库读取数据,并赋值给相关变量
//include ("xxx.html");//加载对应的商品详情页模板
$content = ob_get_contents();//把详情页内容赋值给$content变量
file_put_contents($goods_statis_file, $content);//写入内容到对应静态文件中
ob_end_flush();//发送内部缓冲区的内容到浏览器,删除缓冲区的内容,关闭缓冲区。
}

猜你喜欢

转载自www.cnblogs.com/jiangshiguo/p/10996362.html