產生靜態頁面是php中來減少伺服器負載與seo網站最佳化一個不錯的選擇,所以php產生靜態頁面功能是幾乎所有php程式員必須瞭解並掌握的一個知識點,下面我來給大家介紹php產生靜態頁面原理分析吧,有需要瞭解的朋友可進入參考。
產生html原理分析
我們把要產生的標籤寫成一個模板檔案,然後再利用php讀取把指定標籤替換成我們要替換 內容就可以了,現在主流的dedecms系統也是這麼做的
產生靜態頁面代碼。
模板即尚未填充內容html檔案。例如:
| 代碼如下 |
複製代碼 |
temp.html { title } this is a { file } fileArray;s templets
templetest.php $title = "拓邁國際測試模板"; $file = "TwoMax Inter test templet, author:Matrix@Two_Max"; $fp = fopen ("temp.html","r"); $content = fread ($fp,filesize ("temp.html")); $content .= str_replace ("{ file }",$file,$content); $content .= str_replace ("{ title }",$title,$content); echo $content; ?> |
這樣一個超簡單的php產生靜態頁面的功能就實現了,但實現應用中這個不實用的,下面我介紹一個從資料庫到產生執行個體。
1.建立測試資料庫test,建立user表如下(自己插入幾條測試資料庫):
| 代碼如下 |
複製代碼 |
CREATE TABLE IF NOT EXISTS `news` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(128) DEFAULT NULL, `content` text, `time` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;
|
2.建立串連資料檔案conn.php
| 代碼如下 |
複製代碼 |
$dsn = "mysql:host=localhost;dbname=test;"; $user = "root"; $password = ""; try{ $dbh = new PDO($dsn,$user,$password); }catch(PDOException $e){ echo "串連失敗".$e->getMessage(); } ?> |
3.顯示新聞列表(news.php),注意,其串連為靜態html串連,這時還沒產生,當然連結打不開:
| 代碼如下 |
複製代碼 |
添加文章
require_once "conn.php"; $sql = "select * from news"; foreach($dbh->query($sql) as $row){ echo "{$row['title']}----修改文章 "; } ?> |
4.添加修改文章頁面:
| 代碼如下 |
複製代碼 |
//擷取修改的內容 if($_GET['id']){ require_once "conn.php"; $sql = "select * from news where id={$_GET['id']}"; $res = $dbh->query($sql)->fetch(); } ?>
|
5.用於產生靜態檔案的頁面模板template.html
| 代碼如下 |
複製代碼 |
{title}
{title}發表於{time}
{content}
|
6.action.php當然是用來產生和更新靜態檔案的:
| 代碼如下 |
複製代碼 |
//表單處理操作 header("content-type:text/html;charset=utf-8"); require_once 'conn.php'; $title = $_POST['title']; $content = $_POST['content']; $time = time(); if($_POST['submit']=='添加'){ $sql = "insert into news values('','$title','$content',$time)"; $dbh->query($sql); $id = $dbh->lastInsertId(); $filename = "news_{$id}.html"; $fp_tmp = fopen("template.html","r"); $fp_html = fopen($filename,"w"); while(!feof($fp_tmp)){ $row = fgets($fp_tmp); $row = replace($row,$title,$content,date('Y-m-d H:i:s',$time)); fwrite($fp_html,$row); } fclose($fp_tmp); fclose($fp_html); echo "添加成功並產生靜態檔案"; }else{ $sql = "update news set title = $title , content = $content ,time = $time where id ={$_POST['id']}"; $dbh->query($sql); $filename = "news_{$_POST['id']}.html"; @unlink($filename); $fp_tmp = fopen("template.html","r"); $fp_html = fopen($filename,"w"); while(!feof($fp_tmp)){ $row = fgets($fp_tmp); $row = replace($row,$title,$content,date('Y-m-d H:i:s',$time)); fwrite($fp_html,$row); } fclose($fp_tmp); fclose($fp_html); echo "更新成功並更新靜態檔案"; } //逐行替換函數 function replace($row,$title,$content,$time){ $row=str_replace("{title}",$title,$row); $row=str_replace("{content}",$content,$row); $row=str_replace("{time}",$time,$row); return $row; } ?>
|
這樣一個完整生php產生靜態頁面的系統就完成了。
http://www.bkjia.com/PHPjc/633071.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633071.htmlTechArticle產生靜態頁面是php中來減少伺服器負載與seo網站最佳化一個不錯的選擇,所以php產生靜態頁面功能是幾乎所有php程式員必須瞭解並掌握的一個...