頁面靜態(tài)化,顧名思義是將動態(tài)的PHP轉化為靜態(tài)的Html,流程如下圖
![](/d/20211017/c91520a3523c3c0455034d31aef4f5bf.gif)
用戶訪問index.php,如果存在index.html且在有效期內(nèi),則直接輸出index.html,否則去生成index.html
file_put_contents()輸出靜態(tài)文件
ob_start()開啟PHP緩沖區(qū)
ob_get_contents()獲取緩沖區(qū)內(nèi)容
ob_clean()清空緩沖區(qū)
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 {
// 引入數(shù)據(jù)庫配置
require_once "./config/database.php";
// 引入Medoo類庫
require_once "./libs/medoo.php";
// 實例化db對象
$db = new medoo($config);
// 獲取數(shù)據(jù)
$users = $db->select('user', ['uid', 'username', 'email']);
// 引入模板
require_once "./templates/index.php";
// 寫入html
file_put_contents('./html/index.html', ob_get_contents());
}
您可能感興趣的文章:- PHP實現(xiàn)HTML頁面靜態(tài)化的方法
- PHP實現(xiàn)頁面靜態(tài)化的超簡單方法
- ThinkPHP 3.2.3實現(xiàn)頁面靜態(tài)化功能的方法詳解
- 使用ob系列函數(shù)實現(xiàn)PHP網(wǎng)站頁面靜態(tài)化
- PHP 實現(xiàn)頁面靜態(tài)化的幾種方法
- 詳解php實現(xiàn)頁面靜態(tài)化原理
- 利用php的ob緩存機制實現(xiàn)頁面靜態(tài)化方法
- PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實現(xiàn)方法
- php實現(xiàn)頁面純靜態(tài)的實例代碼
- PHP將整個網(wǎng)站生成HTML純靜態(tài)網(wǎng)頁的方法總結
- 基于php偽靜態(tài)的實現(xiàn)詳細介紹
- PHP頁面靜態(tài)化——純靜態(tài)與偽靜態(tài)用法詳解