2、首页静态化
首页静态化,可通过ob缓存,通过后台动态页面定时生成首页内容,替换旧页面
两个技术:ob缓存控制页面输出到一个html静态页面中、模板替换技术把模板替换成有内容的特定页面
简单代码:
数据库 news表 字段 id title content
manage.html
View Code1 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 Code1 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 / br / 11 br / 12 内容: textarea cols="40" rows="5" /textarea 13 br / 14 input / 15 input / 16 /form 17 /body 18 /html
news.tpl
View Code1 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) 8 die('db connect failed');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);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);25 fclose($$f);26 fclose($$fnews);27 echo '添加新闻成功 a href="manage.html" 返回管理新闻 /a || a href="../'.$$file_html.'" 查看新闻 /a ';29 } elseif ($$_GET['action'] === 'u') {30 echo 'upadte';31 } elseif ($$_GET['action'] === 'd') {32 echo 'del';35 } else {36 echo '参数错误 a href="manage.html" 返回管理新闻 /a ';40 ? newsList.php
View Code 1 ?php 3 //列出新闻列表 4 $$con = mysql_connect('127.0.0.1', 'root', '111111'); 6 if (!$$con) 8 die('fail');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))21 echo " tr td {$$row['id']} /td td {$$row['title']} /td td a href='news_id_{$$row['id']}.html' 查看详情 /a /td /tr \n";23 echo " /table \n";24 $$newslist = ob_get_clean();25 file_put_contents('../index.html', $$newslist);27 mysql_free_result($$res);28 mysql_close($$con);30 echo '更新成功! a href="../index.html" 查看首页 /a || a href="manage.html" 返回管理新闻 /a ';32 ? View Code
1 ?php 2 //查看新闻列表 3 $$con = mysql_connect('127.0.0.1', 'root', '111111'); 5 if (!$$con) 7 die('fail'); 9 mysql_select_db('test', $$con);10 $$sql = "select * from news";11 $$res = mysql_query($$sql, $$con);13 //显示新闻内容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))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 ";28 echo " /table ";30 mysql_free_result($$res);31 mysql_close($$con);33 ?