頁面靜態化,顧名思義是將動態的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實現HTML頁面靜態化的方法
- PHP實現頁面靜態化的超簡單方法
- ThinkPHP 3.2.3實現頁面靜態化功能的方法詳解
- 使用ob系列函數實現PHP網站頁面靜態化
- PHP 實現頁面靜態化的幾種方法
- 詳解php實現頁面靜態化原理
- 利用php的ob緩存機制實現頁面靜態化方法
- PHP單例模式數據庫連接類與頁面靜態化實現方法
- php實現頁面純靜態的實例代碼
- PHP將整個網站生成HTML純靜態網頁的方法總結
- 基于php偽靜態的實現詳細介紹
- PHP頁面靜態化——純靜態與偽靜態用法詳解