加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

实例讲解PHP页面静态化

发布时间:2020-12-12 22:25:05 所属栏目:PHP教程 来源:网络整理
导读:页面静态化,顾名思义是将动态的PHP转化为静态的Html,流程如下图 用户访问index.php,如果存在index.html且在有效期内,则直接输出index.html,否则去生成index.html file_put_contents()输出静态文件 ob_start()开启PHP缓冲区 ob_get_contents()获取缓冲区

页面静态化,顾名思义是将动态的PHP转化为静态的Html,流程如下图

用户访问index.php,如果存在index.html且在有效期内,则直接输出index.html,否则去生成index.html

file_put_contents()输出静态文件

ob_start()开启PHP缓冲区

ob_get_contents()获取缓冲区内容

ob_clean()清空缓冲区

ob_get_clean()相当于ob_get_contents()+ob_clean()

代码示例

if (file_exists('./html/index.html') && time() - filectime('./html/index.html') < 30) {
require_once './html/index.html';
} else {
// 引入数据库配置
require_once "./config/database.php";
// 引入Medoo类库
require_once "./libs/medoo.php";
// 实例化db对象
$db = new medoo($config);
// 获取数据
$users = $db->select('user',['uid','username','email']);
// 引入模板
require_once "./templates/index.php";
// 写入html
file_put_contents('./html/index.html',ob_get_contents());
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读