生成静态页面是php中来减少服务器负载与seo网站优化一个不错的选择,所以php生成静态页面功能是几乎所有php程序员必须了解并掌握的一个知识点,下面我来给大家介绍php生成静态页面原理分析吧.
生成html原理分析
我们把要生成的标签写成一个模板文件,然后再利用php读取把指定标签替换成我们要替换内容就可以了,现在主流的dedecms系统也是这么做的
生成静态页面代码,模板即尚未填充内容html文件,例如如下代码:temp.html
- <HTML>
- <TITLE>{ title }</TITLE>
- <BODY>
- this is a { file } fileArray;s templets
- </BODY>
- </HTML>
templetest.php
- <?php
- $title = "拓迈国际测试模板";
- $file = "TwoMax Inter test templet,<br>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,代码如下:
- <?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连接,这时还没生成,当然链接打不开,代码如下:
- <meta http-equiv="content-type" content="text/html;charset=utf-8" />
- <a href="add.php">添加文章</a>
- <hr>
- <?php
- require_once "conn.php";
- $sql = "select * from news";
- foreach($dbh->query($sql) as $row){
- echo "<a href='news_{$row['id']}.html'>{$row['title']}</a>----<a href='add.php?id={$row['id']}'>修改文章</a><br>";
- }
- ?>
4.添加修改文章页面,代码如下:
- <meta http-equiv="content-type" content="text/html;charset=utf-8" />
- <?php
- //获取修改的内容
- if($_GET['id']){
- require_once "conn.php";
- $sql = "select * from news where id={$_GET['id']}";
- $res = $dbh->query($sql)->fetch();
- }
- ?>
- <form action="action.php" method="post">
- 标题:<input type="text" name="title" value="<?=@$res['title']?>"/><br/>
- 内容:<textarea name="content" col=40 row=4><?=@$res['content']?></textarea><br/>
- <input type="hidden" name="id" value="<?=$_GET['id']?>" />
- <input type="submit" name="submit" value="<?php echo $_GET['id'] ? '修改' : '添加'?>" />
- </form>
5.用于生成静态文件的页面模板template.html,代码如下:
- <html>
- <head>
- <title>{title}</title>
- <meta http-equiv="content-type"content="text/html; charset=UTF-8"/>
- </head>
- <body>
- {title}发表于{time}
- <hr>
- {content}
- </body>
- </html>
6.action.php当然是用来生成和更新静态文件的,代码如下:
- <?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;
- }
- ?>
新闻热点
疑难解答