真静态技术就是在添加新闻时候产生一个html静态文件,等再次修改新闻时候,再重新产生html静态文件,这样保证html静态文件和数据表内容完全一致。
实例如下:
(1)操作数据库代码页:ConnDB.class.php
<?php /** * Created by PhpStorm. * User: 58 * Date: 2016/8/5 * Time: 8:57 */ class ConnDB{ private static $host = '127.0.0.1'; private static $username = 'root'; private static $password = '7758521Lhy'; private static $db = 'test'; private $conn = null; public function __construct(){ $this->conn = new MySQLi(self::$host,self::$username,self::$password,self::$db); if(!$this->conn){ echo '数据库连接错误:'.$this->conn->connect_error; exit(); } $this->conn->query("set names utf-8"); } public function execute_dql($sql){ $rs = $this->conn->query($sql) or die('查询出错!'.$this->conn->error); $rsList = array(); if($rs){ while($row = $rs->fetch_assoc()){ $rsList[] = $row; } } $rs->free(); return $rsList; } public function execute_dml($sql){ $rs = $this->conn->query($sql); if(!$rs){ $flag = 0; }else if($this->conn->affected_rows){ $flag = 1; }else{ $flag = 2; } return $flag; } public function getInsertId(){ return $this->conn->insert_id; } public function clossDB(){ if($this->conn){ $this->conn->close(); } } }
(2)产生文件名字代码页getHtmlName.php
<?php /** * Created by PhpStorm. * User: 58 * Date: 2016/8/9 * Time: 21:29 */ function getHtmlName($id){ return "news_id".$id.".html"; }
(3)显示新闻列表页面news_list.php
<?php /** * Created by PhpStorm. * User: 58 * Date: 2016/8/5 * Time: 15:31 */ header("Content-Type:text/html;charset=utf-8"); require_once "getHtmlName.php"; require_once "ConnDB.class.php"; $conn = new ConnDB(); $sql = "select * from news"; $rs = $conn->execute_dql($sql); $conn->clossDB(); echo " <a href='add_news.php'>发布文章</a> <table border='1'> <tr><th>id</th><th>标题</th><th>详细内容</th><th>修改</th></tr>"; foreach($rs as $row){ $url = getHtmlName($row['id']); echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href= {$url}>详细内容</a></td><td><a href= update_news.php?id={$row['id']}>修改</a></td></tr>"; } echo " </table> ";
(4)添加新闻的页面 add_news.php
<?php /** * Created by PhpStorm. * User: 58 * Date: 2016/8/5 * Time: 15:34 */ ?> <center>发布新闻</center> <form method="post" action="newsAction.php"> <table> <tr><th>新闻标题:</th><td><input type="text" name="title"></td></tr> <tr><th>新闻内容:</th><td><input type="text" name="content"></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="发布"></td></tr> </table> <input type="hidden" name="opt" value="add"> </form>
(5)修改新闻的代码页 update_news.php
<?php /** * Created by PhpStorm. * User: 58 * Date: 2016/8/9 * Time: 21:48 */ require_once "ConnDB.class.php"; $id = $_GET['id']; $conn = new ConnDB(); $rs = $conn->execute_dql("select * from news where id = {$id}"); $conn->clossDB(); if($rs && $row = $rs[0]){ ?> <form action="newsAction.php" method="post"> <input type="hidden" name="id" value="<?=$row['id']?>"> <input type="hidden" name="opt" value="up"> 标题:<input type="text" name="title" value="<?=$row['title']?>"><br/> 内容:<input type="text" name="content" value="<?=$row['content']?>"><br/> <input type="submit" value="修改" /> </form> <?php } ?>
(6)处理添加或修改函数代码页:newsAction.php
<?php /** * Created by PhpStorm. * User: 58 * Date: 2016/8/5 * Time: 15:37 */ require_once "ConnDB.class.php"; require_once "getHtmlName.php"; $conn = new ConnDB(); $opt = $_POST['opt']; $title = $_POST['title']; $content = $_POST['content']; if($opt == 'add'){ $sql = "insert into news(title,content) VALUES ('{$title}','{$content}')"; $rs = $conn->execute_dml($sql); $id = $conn->getInsertId(); }else if($opt == 'up'){ $id = $_POST['id']; $sql = "update news set title='{$title}',content='{$content}' where id = '{$id}'"; $rs = $conn->execute_dml($sql); } $conn->clossDB(); if(!$id){ exit(); } $file_name = getHtmlName($id); if(!$rs){ echo '执行失败!'; exit(); }else if($rs == 1){ echo '执行成功!'; }else{ echo '执行成功,但是没有行改变!'; } $fp_tmp = fopen("template.tpl","r"); $fp_html_file = fopen($file_name,"w"); while(!feof($fp_tmp)){ $row = fgets($fp_tmp); $row = str_replace("%title%",$title,$row); $row = str_replace("%content%",$content,$row); fwrite($fp_html_file,$row); } fclose($fp_tmp); fclose($fp_html_file);
(7)生成静态文件的模板template.tpl
<html> <head> <meta http-equiv="content-type=text/html;charset=utf-8"/> <title>%title%</title> </head> <body> <h1>%title%</h1> <hr/> <pre>%content%</pre> </body> </html>
真静态技术解决了php缓存机制的缺陷,它的优点有三个:
(1)减少服务器对数响应的负荷;
(2)加载不用调动数据库,响应速度快;
(3)便于优化引擎。
但是真静态技术也存在一定缺陷。比如:
(1)空间占用比较大;
(2)生成的文件多了,服务器对html文件的响应负担也比较重。
一个系统使用真静态进行页面静态化,需要生成海量页面静态文件,可以考虑使用伪静态来处理。可以继续优化: