全站静态化技术
1、新闻单页静态化
- 设置tpl模板页,模板页里面有占位符,通过后台添加新闻,同时通过替换模板页生成一个新的新闻静态html页面,以供访问
- 修改页面信息时,操作数据更新至数据库,同时更新html文件页面,删除信息时,数据库和html页面同时删除
2、首页静态化
首页静态化,可通过ob缓存,通过后台动态页面定时生成首页内容,替换旧页面
两个技术:ob缓存控制页面输出到一个html静态页面中、模板替换技术把模板替换成有内容的特定页面
简单代码:
数据库 news表 字段 id title content
manage.html
View Code
1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 4 <title>管理新闻</title> 5 </head> 6 <body> 7 <h1>管理新闻</h1> 8 <hr /> 9 <a href="add.html">添加新闻</a>|| 10 <a href="newsList.php">更新首页</a>|| 11 <a href="showNews.php">查看新闻列表</a> 12 </body> 13 </html>
add.html
View Code
1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 4 <title>添加新闻</title> 5 </head> 6 <body> 7 <h1>添加新闻</h1> 8 <hr /> 9 <form action="newsAction.php" method="post"> 10 标题:<input type="text" id="title" name="title" /><br /> 11 <br /> 12 内容:<textarea id="content" name="content" cols="40" rows="5"></textarea> 13 <br /> 14 <input type="submit" value="提交" id="sub" name="sub"/> 15 <input type="hidden" value="add" id="action" name="action"/> 16 </form> 17 </body> 18 </html>
news.tpl
View Code
1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 4 <title>新闻 - %title%</title> 5 </head> 6 <body> 7 <h1>%title%</h1> 8 <hr /> 9 %content% 10 </body> 11 </html>
newsAction.php
View Code
1 <?php 2 //新闻控制页 3 header('content-type:text/html;charset=utf-8'); 4 if (isset($_REQUEST['action'])) { 5 $con = mysql_connect('127.0.0.1', 'root', '111111'); 6 if (!$con) 7 { 8 die('db connect failed'); 9 } 10 mysql_select_db('test', $con); 11 if (isset($_POST['action']) && $_POST['action'] === 'add') { 12 $sql = "insert into news (title, content) values ('{$_POST['title']}','{$_POST['content']}')"; 13 $res = mysql_query($sql, $con); 14 $addid = mysql_insert_id($con); 15 mysql_close($con); 16 17 $file_html = 'news_id_'.$addid.'.html'; 18 $f = fopen('news.tpl', 'r'); 19 $fnews = fopen('../'.$file_html, 'w'); 20 while (!feof($f)) { 21 $str = fgets($f); 22 $str = str_replace(array('%title%', '%content%'), array($_POST['title'], $_POST['content']), $str); 23 fwrite($fnews, $str); 24 } 25 fclose($f); 26 fclose($fnews); 27 echo '添加新闻成功 <a href="manage.html">返回管理新闻</a>||<a href="../'.$file_html.'">查看新闻</a>'; 28 29 } elseif ($_GET['action'] === 'u') { 30 echo 'upadte'; 31 } elseif ($_GET['action'] === 'd') { 32 echo 'del'; 33 } 34 35 } else { 36 echo '参数错误 <a href="manage.html">返回管理新闻</a>'; 37 } 38 39 40 ?>
newsList.php
View Code
1 <?php 2 3 //列出新闻列表 4 $con = mysql_connect('127.0.0.1', 'root', '111111'); 5 6 if (!$con) 7 { 8 die('fail'); 9 } 10 mysql_select_db('test', $con); 11 $sql = "select * from news"; 12 $res = mysql_query($sql, $con); 13 header('content-type:text/html;charset=utf-8'); 14 ob_start(); 15 echo '<meta http-equiv="content-type" content="text/html;charset=utf-8"/>\n'; 16 echo "<h1>新闻列表</h1>\n"; 17 echo "<table>\n"; 18 echo "<tr><td>id</td><td>标题</td><td>查看详情</td></tr>\n"; 19 while ($row = mysql_fetch_assoc($res)) 20 { 21 echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href='news_id_{$row['id']}.html'>查看详情</a></td></tr>\n"; 22 } 23 echo "</table>\n"; 24 $newslist = ob_get_clean(); 25 file_put_contents('../index.html', $newslist); 26 27 mysql_free_result($res); 28 mysql_close($con); 29 30 echo '更新成功!<a href="../index.html">查看首页</a>||<a href="manage.html">返回管理新闻</a>'; 31 32 ?>
showNews.php
View Code
1 <?php 2 //查看新闻列表 3 $con = mysql_connect('127.0.0.1', 'root', '111111'); 4 5 if (!$con) 6 { 7 die('fail'); 8 } 9 mysql_select_db('test', $con); 10 $sql = "select * from news"; 11 $res = mysql_query($sql, $con); 12 13 //显示新闻内容 14 15 echo '<meta http-equiv="content-type" content="text/html;charset=utf-8"/>'; 16 echo "<h1>新闻列表</h1>"; 17 echo "<table>"; 18 echo "<tr><td>id</td><td>标题</td><td>查看详情</td><td>操作</td></tr>"; 19 while ($row = mysql_fetch_assoc($res)) 20 { 21 echo "<tr> 22 <td>{$row['id']}</td> 23 <td>{$row['title']}</td> 24 <td><a href='../news_id_{$row['id']}.html'>查看详情</a></td> 25 <td><a href='newsAction.php?action=d&id={$row['id']}'>删除</a>|<a href='newsAction.php?action=u&id={$row['id']}'>修改</a></td> 26 </tr>"; 27 } 28 echo "</table>"; 29 30 mysql_free_result($res); 31 mysql_close($con); 32 33 ?>
本文来自博客园,作者:Caps,转载请注明原文链接:https://www.cnblogs.com/caps/archive/2013/03/04/2943423.html