生成静态页面是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;
?>
|
temp.html
{ title }
代码如下 |
复制代码 |
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 ;
|
this is a { file } fileArray;s templets
代码如下 |
复制代码 |
$dsn = "mysql:host=localhost;dbname=test;";
$user = "root";
$password = "";
try{
$dbh = new PDO($dsn,$user,$password);
}catch(PDOException $e){
echo "连接失败".$e->getMessage();
}
?> |
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}
|
{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;
}
?>
|
|
复制代码
|
|
//表单处理操作
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生成静态页面的系统就完成了。