华域联盟 PHP 实例讲解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());
}

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » 实例讲解PHP页面静态化

转载请保留出处和原文链接:https://www.cnhackhy.com/44509.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部