首页 > 开发 > PHP > 正文

PHP页面静态化两种实现方案(模板标签)

2024-05-04 22:00:07
字体:大 中 小
来源:转载
供稿:网友

静态化页面的好处就不说了,现在我们下文要介绍的实现方法就是利用定义的模板页面和标签然后再利用php读取模板页面之后进入分析替换就可以了,具体我们来看两个例子.

方案:使用模板替换技术(没有时间延迟),代码如下:

  1. /* 
  2. |------------------ 
  3. | <www.Vevb.com> 
  4. |------------------ 
  5. */ 
  6. $oper = $_POST['oper'];//添加操作 
  7. if($oper === 'add') 
  8. { 
  9.     $title = $_POST['title']; 
  10.     $content = $_POST['content']; 
  11.      
  12.     //如果严格按MVC,这里应该调用model了 
  13.     $con = mysql_connect('localhost', 'root', '123456'); 
  14.     if(!$con) 
  15.     { 
  16.         die('连接失败!'); 
  17.     } 
  18.     mysql_select_db('news', $con); 
  19.     $sql = "insert into question(null, '$title', '$content', '')"; 
  20.     if(mysql_query($sql, $con)) 
  21.     { 
  22.         //1.生成静态文件  
  23.         $id = mysql_insert_id(); 
  24.         $html_filename = 'news-id'.$id.'.html'; 
  25.         $html_fp = fopen($html_filename, 'w'); 
  26.          
  27.         //2.把模板文件读取(news.html) 
  28.         $fp = fopen('news.tpl', 'r'); 
  29.         //r 只读方式打开; r+ 读写方式打开; w 写入方式打开:文件内容将被清空!如果文件不存在将创建; a 以追加的方式打开 
  30.          
  31.         //3.循环读取 
  32.         //如果没有读到文件的最后,就一直读取 
  33.         while(!feof($fp)) 
  34.         { 
  35.             //一行行读 
  36.             $row = fgets($fp); 
  37.             //把占位符替换掉 => 可以自定义完整的替换规则函数 
  38.             $row = str_replace('%title%', $title, $row);//如果不重新赋值$row, $row值不会改变 
  39.             $row = str_replace('%content%', $content, $row); 
  40.              
  41.             fwrite($html_fp, $row);//4.将内容写入静态文件 
  42.         } 
  43.         //5.文件必须关闭 
  44.         fclose($html_fp); 
  45.         fclose($fp); 
  46.          
  47.         echo "添加成功。<a href='newslist.php'>点击查看新闻!</a>"; 
  48.     } 
  49.     else 
  50.     { 
  51.         die('添加失败!'); 
  52.     } 
  53. } 

此时在新闻列表内,点击查看详情的链接,可以改成生成的静态页面地址,直接进入静态文件,代码如下:

  1. <html>  
  2.     <head>  
  3.         <meta charset="utf-8" />  
  4.         <title>%title%</title>  
  5.     </head>  
  6.     <body>  
  7.         <h1>%title%</h1>  
  8.         <pre>%content%</pre>  
  9.     </body>  
  10. </html> 

方案:如果静态文件存在,且生成时间30秒内,直接返回静态页面(有时间延迟),代码如下:

  1. /* 
  2. |------------------ 
  3. | <www.Vevb.com> 
  4. |------------------ 
  5. */ 
  6. header('content-type:text/html;charset=utf-8'); 
  7. $id = $_GET['id'] ? intval($_GET['id']) : ''; 
  8. if($id === '') die('请输入要查询的新闻id!'); 
  9. $html_file = "news-id-".$id.".html"; 
  10. //1.主要代码 
  11. if(file_exists($html_file) && filemtime($html_file) + 30 >= time()) 
  12. { 
  13.     echo '静态页面:'; 
  14.     echo file_get_contents($html_file);exit; 
  15. } 
  16. //这里也可以使用DB工具类 
  17. $con = mysql_connect('localhost', 'root', '123456'); 
  18. if(!$con) 
  19. { 
  20.     die('连接失败!'); 
  21. } 
  22. mysql_select_db('testdb', $con); 
  23. $sql = "select * from bo_question where question_id = $id"; 
  24. $res = mysql_query($sql, $con); 
  25. if($row = mysql_fetch_assoc($res)) 
  26. { 
  27.     ob_start();//2.启动ob缓存 
  28.     header('content-type:text/html;charset=utf-8'); 
  29.     echo '<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /></head>'; 
  30.     echo '<table style="border:1px solid red;" cellspacing="0" width="400px" height="200px">'; 
  31.     echo '<tr><td>问题详细内容</td></tr>'; 
  32.     echo "<tr><td>标题:{$row['question_title']}</td></tr>"; 
  33.     echo "<tr><td>详细:{$row['question_detail']}</td></tr>"; 
  34.     echo '</table>'; 
  35.     $ob_str = ob_get_contents(); 
  36.     //3.把ob_str保存到一个静态文件页面,取文件名有讲究:1.唯一标识该新闻 2.利于seo 
  37.     file_put_contents("news-id-".$id.".html", $ob_str); 
  38.    
  39.   //关闭数据库连接(非必须; 非长连接下,脚本执行完会自动关闭)  
  40.   mysql_close($con); 
  41. }else{ 
  42.     echo '没有查询到资源!'; 
  43. } 

从上面看我们头一种生成静态页面的方法更合适于页面的后期维护哦,后者是比较难维护的哦,推荐使用第一种办法.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表