首頁  >  文章  >  後端開發  >  PHP頁面靜態化的實例詳解

PHP頁面靜態化的實例詳解

php中世界最好的语言
php中世界最好的语言原創
2018-03-05 14:29:181397瀏覽

頁面靜態化,顧名思義是將動態的PHP轉換為靜態的Html,以下這篇文章小編將為大家介紹PHP頁面靜態化的原理以及相關方法,有需要的朋友可以參考一下。

具體流程如下圖

使用者存取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(&#39;./html/index.html&#39;) && time() - filectime(&#39;./html/index.html&#39;) < 30) {
 require_once &#39;./html/index.html&#39;;
} else {
 // 引入数据库配置
 require_once "./config/database.php";
 // 引入Medoo类库
 require_once "./libs/medoo.php";
 // 实例化db对象
 $db = new medoo($config);
 // 获取数据
 $users = $db->select(&#39;user&#39;, [&#39;uid&#39;, &#39;username&#39;, &#39;email&#39;]);
 // 引入模板
 require_once "./templates/index.php";
 // 写入html
 file_put_contents(&#39;./html/index.html&#39;, ob_get_contents());
}

相關推薦:

php的快取機制實作頁面靜態化程式碼分享

##PHP頁面靜態化的實作程式碼

php實作頁面靜態化影片教學資料推薦


#

以上是PHP頁面靜態化的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn