PHP页面的静态化很有必要,尤其是在CMS系统中,一些内容一旦生成,基本上不会有变化,这时如果用html将页面静态化,无疑会减少服务其解析PHP页面的负担。以下是看书学来的PHP静态化技术,记录之以备不时之需。
无论是利用框架还是简单的脚本,原理基本一致:就是利用PHP进行文件操作,替换html模板中的动态元素。
用Replace函数即php的str_replace函数将模版文件中读取的内容中的关键字替换成变量中的内容,从而实现简单的模板分离。
1.新增与回显
insert.htm文件代码:在标题和内容框中分别输入:“这是标题”和“这是内容”
- <html>
- <head>
- <title>添加一条新记录</title>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- </head>
-
- <body>
- <p><h1>添加一条新记录</h1></p>
- <form name="form1" method="post" action="insert.php">
- <table width="500" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td>标题</td>
- <td><input name="title" type="text" id="title"></td>
- </tr>
- <tr>
- <td>内容</td>
- <td><textarea name="body" cols="35" rows="5" id="body"></textarea></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="Submit" value="Submit">
- <input type="reset" name="Submit2" value="Reset"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
模板文件template.htm:
- <html>
- <head>
- <title>%title%</title>
- </head>
- <body>
- <H1>%title%</H1>
- <hr>
- <pre>%body%</pre>
- </body>
- </html>
php
- <?php
-
- function Replace($row, $title='', $body='')
- {
-
- $row = str_replace("%title%", $title, $row);
- $row = str_replace("%body%", $body, $row);
-
- return $row;
- }
-
- @mysql_connect("localhost", "root","1981427")
- or die("数据库服务器连接失败");
- @mysql_select_db("test")
- or die("数据库不存在或不可用");
-
- $title = $_POST['title'];
- $body = $_POST['body'];
-
-
- $filename = 'S'.date("YmdHis").'.htm';
-
- $query = mysql_query("insert into news values('$title', '$body', '$filename')");
-
- if($query)
- {
-
- $f_tem = fopen("template.htm","r");
-
- $f_new = fopen('new.htm',"w");
-
-
- while(!feof($f_tem))
- {
- $row = fgets($f_tem);
- $row = Replace($row, $title, $body);
- fwrite($f_new, $row);
- }
-
- fclose($f_new);
- fclose($f_tem);
-
- echo "数据插入成功";
- }
- else
- echo "数据插入失败";
- mysql_close();
- ?>
生成新的html页:new.html
- <html>
- <head>
- <title>文章标题</title>
- </head>
- <body>
- <H1>文章标题</H1>
- <hr>
- <pre>这里是文章主体</pre>
- </body>
- </html>
2.修改与回显edit.php
- <?php
-
- @mysql_connect("localhost", "root","orbit")
- or die("数据库服务器连接失败");
- @mysql_select_db("test")
- or die("数据库不存在或不可用");
- $query = mysql_query("select * from news where title='这是标题'");
-
- $row = mysql_fetch_array($query);
- ?>
- <html>
- <head>
- <title>修改一条新记录</title>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- </head>
-
- <body>
- <p><h1>修改一条新记录</h1></p>
- <form name="form1" method="post" action="mdf_to_staitc.php">
- <table width="500" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td>标题</td>
- <td><input name="title" type="text" id="title" value="<?php echo $row['title']?>"></td>
- </tr>
- <tr>
- <td>内容</td>
- <td><textarea name="body" cols="35" rows="5" id="body"><?php echo $row['body']?></textarea></td>
- </tr>
- <tr>
- <input name="filename" type="hidden" value="<?php echo $row['filename']?>">
- <td colspan="2"><input type="submit" name="Submit" value="Submit">
- </tr>
- </table>
- </form>
- </body>
- </html>
php
- <?php
-
- function Replace($row, $title='', $body='')
- {
-
- $row = str_replace("%title%", $title, $row);
- $row = str_replace("%body%", $body, $row);
-
- return $row;
- }
-
- @mysql_connect("localhost", "root","orbit")
- or die("数据库服务器连接失败");
- @mysql_select_db("test")
- or die("数据库不存在或不可用");
-
- mysql_query("update news set title='".$_POST['title']."',body='".$_POST['body']."' where filename='".$_POST['filename']."' ");
- $query = mysql_query("select * from news where filename='".$_POST['filename']."' ");
-
- while($record = mysql_fetch_array($query))
- {
- echo "<pre>";
- print_r($record);
-
- $f_tem = fopen("template.htm","r");
-
- $f_new = fopen($record['filename'],"w");
-
- while(!feof($f_tem))
- {
- $row = fgets($f_tem);
- $row = Replace($row, $record['title'], $record['body']);
- fwrite($f_new, $row);
- }
-
- fclose($f_new);
- fclose($f_tem);
- }
- mysql_close();
- ?>
-
- mysql_close();
- ?>
3.一般的CMS都会记录内容被浏览的信息,例如浏览次数或者浏览者的IP信息等,静态页面要记录这些信息,可以在模板中加入一个长宽都为0的图片,指向计数脚本。
以记录浏览次数为例:
<img width='0' height='0' src='counter.php?fileID=S001' />
这样,计数操作可以放到counter.php中进行,又不会破坏网页的静态性。
<?php
- $query = mysql_query("update counter set count+=1 where fileid='".$fileid."' ");