博主信息
php开发大牛
博文
142
粉丝
5
评论
0
访问量
84371
积分:0
P豆:304

实例讲解PHP页面静态化

2018年04月20日 15:15:37阅读数:816博客 / php开发大牛/ php开发进阶

页面静态化,顾名思义是将动态的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()

代码示例

<?php

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());
}


版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • 这篇文章主要介绍了PHP之纯与伪用法,结合形式分析了PHP——纯与伪相关原理、现方法与相关操作注意事项,需要的朋友可以参考下
    php缓存主要用到的是ob系列函数,如【ob_start(),ob_end_flush(),ob_get_contents() 】,今天我们来谈谈使用这些函数来php网站