頁(yè)面靜態(tài)化,顧名思義是將動(dòng)態(tài)的PHP轉(zhuǎn)化為靜態(tài)的Html,流程如下圖
用戶訪問(wèn)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()相當(dāng)于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ù)庫(kù)配置 require_once "./config/database.php"; // 引入Medoo類庫(kù) require_once "./libs/medoo.php"; // 實(shí)例化db對(duì)象 $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()); }
您可能感興趣的文章:
- ThinkPHP 3.2.3實(shí)現(xiàn)頁(yè)面靜態(tài)化功能的方法詳解
- PHP 實(shí)現(xiàn)頁(yè)面靜態(tài)化的幾種方法
- 利用php的ob緩存機(jī)制實(shí)現(xiàn)頁(yè)面靜態(tài)化方法
- 詳解php實(shí)現(xiàn)頁(yè)面靜態(tài)化原理
- PHP實(shí)現(xiàn)頁(yè)面靜態(tài)化的超簡(jiǎn)單方法
- PHP實(shí)現(xiàn)HTML頁(yè)面靜態(tài)化的方法
- 使用ob系列函數(shù)實(shí)現(xiàn)PHP網(wǎng)站頁(yè)面靜態(tài)化
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過(guò)測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。