頁面靜態(tài)化,顧名思義是將動態(tài)的PHP轉化為靜態(tài)的Html,流程如下圖
用戶訪問index.php,如果存在index.html且在有效期內,則直接輸出index.html,否則去生成index.html
file_put_contents()輸出靜態(tài)文件
ob_start()開啟PHP緩沖區(qū)
ob_get_contents()獲取緩沖區(qū)內容
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使用ActiveMQ實例