實例講解PHP頁面靜態(tài)化
頁面靜態(tài)化,顧名思義是將動態(tài)的PHP轉(zhuǎn)化為靜態(tài)的Html,流程如下圖
用戶訪問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()); }
相關(guān)文章
PHP實現(xiàn)Google plus的好友拖拽分組效果
本篇文章使用PHP和jQuery實現(xiàn)了同樣的好友拖拽添加分組的應用。以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也我的示例會對你的社交網(wǎng)站項目有所幫助。2016-10-10PHP+FFMPEG實現(xiàn)將視頻自動轉(zhuǎn)碼成H264標準Mp4文件
最近做一個在線教學網(wǎng)的項目,需要實現(xiàn)上傳任意格式視頻自動為h264標準視頻,使用html5播放。最終使用PHP+FFMPEG實現(xiàn),在此將詳細解決方案分享給大家!2014-09-09Laravel監(jiān)聽數(shù)據(jù)庫訪問,打印SQL的例子
今天小編就為大家分享一篇Laravel監(jiān)聽數(shù)據(jù)庫訪問,打印SQL的例子,有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10