PHP页面静态化实例分享

小云云
小云云 原创
2023-03-19 22:38:02 1078浏览

页面静态化,顾名思义是将动态的PHP转化为静态的Html,本文主要和大家通过实例讲解了PHP页面静态化的原理以及相关方法,对此有需要的朋友参考下吧。希望能帮助到大家。

流程如下图

用户访问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());
}

相关推荐:

PHP页面静态化的实现代码

ThinkPHP3.2.3页面静态化实现方法

html实现页面静态化的案例


以上就是PHP页面静态化实例分享的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。