PHP使用OB緩存實(shí)現(xiàn)靜態(tài)化功能示例
本文實(shí)例講述了PHP使用OB緩存實(shí)現(xiàn)靜態(tài)化功能。分享給大家供大家參考,具體如下:
實(shí)現(xiàn)步驟
1、創(chuàng)建測試數(shù)據(jù)表并且寫入數(shù)據(jù)
2、實(shí)現(xiàn)后臺的更新操作。使用OB緩存針對每一個內(nèi)容生成對應(yīng)的HTML文件
3、顯示前臺的數(shù)據(jù)信息
具體實(shí)現(xiàn)
①創(chuàng)建測試數(shù)據(jù)表并且寫入數(shù)據(jù)(test.sql文件):
#創(chuàng)建數(shù)據(jù)表 create table news( id int auto_increment, title varchar(100) not null default '', body text, primary key(id) )engine =myisam default charset=utf8; #數(shù)據(jù)寫入 insert into news values(null,'靜態(tài)化','靜態(tài)化可以減少服務(wù)器壓力'),(null,'偽靜態(tài)','偽靜態(tài)能夠滿足SEO優(yōu)化');
②實(shí)現(xiàn)后臺的更新操作(admin.php文件)
<?php //具體的后臺更新 //獲取所有的數(shù)據(jù)信息 mysql_connect('127.0.0.1','root','123456'); mysql_select_db('test'); $sql='select * from news'; $res = mysql_query($sql); while ($row=mysql_fetch_assoc($res)) { //針對每一條數(shù)據(jù)生成html文件 ob_start();//開啟OB緩存 ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>靜態(tài)化介紹</title> </head> <body> <h1><?php echo $row['title']; ?></h1> <div><?php echo $row['body']; ?></div> </body> </html> <?php //獲取OB緩存中的內(nèi)容 $str = ob_get_contents(); //關(guān)閉OB緩存并且清空內(nèi)容。因?yàn)槿绻磺蹇諡g覽器上會看到所有的數(shù)據(jù)結(jié)果 ob_end_clean(); //將信息寫入到文件中 關(guān)于具體的文件目錄及文件名稱需要自定義 //對于在實(shí)際項(xiàng)目中關(guān)于html文件的存儲 一般都會使用年月日的格式存在 file_put_contents($row['id'].'.html',$str); } ?>
③實(shí)現(xiàn)前臺數(shù)據(jù)顯示(list.php文件):
<?php //顯示列表 //獲取所有的數(shù)據(jù)信息 mysql_connect('127.0.0.1','root','123456'); mysql_select_db('test'); $sql='select * from news'; $res = mysql_query($sql); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>靜態(tài)化介紹</title> </head> <body> <h1>顯示列表</h1> <table> <tr> <td>序號</td> <td>標(biāo)題</td> <td>查看</td> </tr> <?php while ($row =mysql_fetch_assoc($res)) {?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['title']; ?></td> <td><a href="<?php echo $row['id'];?>.html" rel="external nofollow" > 查看</a></td> </tr> <?php } ?> </table> </body> </html>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php緩存技術(shù)總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- thinkphp5redis緩存新增方法實(shí)例講解
- PHP ob緩存以及ob函數(shù)原理實(shí)例解析
- 解決PHP Opcache 緩存刷新、代碼重載出現(xiàn)無法更新代碼的問題
- PHP網(wǎng)頁緩存技術(shù)優(yōu)點(diǎn)及代碼實(shí)例
- php加速緩存器opcache,apc,xcache,eAccelerator原理與配置方法實(shí)例分析
- PHP利用緩存處理用戶注冊時的郵箱驗(yàn)證,成功后用戶數(shù)據(jù)存入數(shù)據(jù)庫操作示例
- TP5(thinkPHP框架)實(shí)現(xiàn)后臺清除緩存功能示例
- ThinkPHP3.2.3框架Memcache緩存使用方法實(shí)例總結(jié)
- 簡單實(shí)用的PHP文本緩存類實(shí)例
- PHP緩存系統(tǒng)APCu擴(kuò)展的使用
相關(guān)文章
火車采集器 免費(fèi)版使出收費(fèi)版本功能實(shí)現(xiàn)原理
火車頭免費(fèi)版本不支持采集結(jié)果的外掛處理,比如采用php來輔助處理結(jié)果,而火車頭本身對于正則表達(dá)式的不完整支持,2009-09-09php簡單計算權(quán)重的方法示例【適合抽獎類應(yīng)用】
這篇文章主要介紹了php簡單計算權(quán)重的方法,結(jié)合實(shí)例形式分析了php概率相關(guān)的數(shù)值計算操作技巧,適合抽獎類應(yīng)用,需要的朋友可以參考下2019-06-06