PHP使用OB緩存實現(xiàn)靜態(tài)化功能示例
本文實例講述了PHP使用OB緩存實現(xiàn)靜態(tài)化功能。分享給大家供大家參考,具體如下:
實現(xiàn)步驟
1、創(chuàng)建測試數(shù)據(jù)表并且寫入數(shù)據(jù)
2、實現(xiàn)后臺的更新操作。使用OB緩存針對每一個內(nèi)容生成對應(yīng)的HTML文件
3、顯示前臺的數(shù)據(jù)信息
具體實現(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)化');
②實現(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)容。因為如果不清空瀏覽器上會看到所有的數(shù)據(jù)結(jié)果
ob_end_clean();
//將信息寫入到文件中 關(guān)于具體的文件目錄及文件名稱需要自定義
//對于在實際項目中關(guān)于html文件的存儲 一般都會使用年月日的格式存在
file_put_contents($row['id'].'.html',$str);
}
?>
③實現(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緩存新增方法實例講解
- PHP ob緩存以及ob函數(shù)原理實例解析
- 解決PHP Opcache 緩存刷新、代碼重載出現(xiàn)無法更新代碼的問題
- PHP網(wǎng)頁緩存技術(shù)優(yōu)點及代碼實例
- php加速緩存器opcache,apc,xcache,eAccelerator原理與配置方法實例分析
- PHP利用緩存處理用戶注冊時的郵箱驗證,成功后用戶數(shù)據(jù)存入數(shù)據(jù)庫操作示例
- TP5(thinkPHP框架)實現(xiàn)后臺清除緩存功能示例
- ThinkPHP3.2.3框架Memcache緩存使用方法實例總結(jié)
- 簡單實用的PHP文本緩存類實例
- PHP緩存系統(tǒng)APCu擴(kuò)展的使用
相關(guān)文章
阿里云的WindowsServer2016上部署php+apache
這篇文章主要介紹了阿里云的WindowsServer2016上部署php+apache的相關(guān)資料,需要的朋友可以參考下2018-07-07

