PHP页面静态化的实现

关于静态化,PHP的静态化分为:纯静态和伪静态。其中纯静态又分为:局部纯静态和全部纯静态。这里将的是全部纯静态。

纯静态化实例:

db.php、template.php 、test.php

db.php代码如下:

 1 <?php
 2 header("content-type:text/html;charset=utf-8");
 3 /*
 4  * 单例模式连接数据库
 5  */
 6 class DB{
 7     static private $_instance;    //非public的类的实例的静态成员变量
 8     static private $_connectSource;    //连接数据库返回的资源句柄
 9     private $_dbConfig = array(
10         'host'=>'127.0.0.1',
11         'username'=>'root',
12         'pwd'=>'root',
13         'database'=>'mvc'
14     );
15 
16     private function __construct(){    //非public 的构造函数
17     }
18 
19     static public function getInstance(){    //访问实例的公共静态方法
20         if(!self::$_instance instanceof self){
21             self::$_instance = new self();
22         }
23         return self::$_instance;
24     }
25 
26     public function connect(){
27         if(!self::$_connectSource){
28             //连接mysql服务
29             self::$_connectSource = @mysql_connect($this->_dbConfig['host'],$this->_dbConfig['username'],$this->_dbConfig['pwd']);
30             if(!self::$_connectSource){
31                 //抛出异常
32                 throw new Exception('mysql connect error'.mysql_error());
33             }
34             //选择数据库
35             mysql_select_db($this->_dbConfig['database'],self::$_connectSource);
36             //设置字符集
37             mysql_query('set names "UTF8"',self::$_connectSource);
38         }
39         return self::$_connectSource; //返回资源
40     }
41 }

template.php代码如下:

 1 <!DOCTYPE html>  
 2 <html lang="en">  
 3 <head>  
 4     <meta charset="UTF-8">  
 5     <title>新闻中心</title>  
 6      
 7 </head>  
 8 <body>  
 9     
10     <div class="container">  
11         <h3>新闻列表</h3>  
12         <ul class="list-group">  
13             <?php foreach ($news as $key => $value) { ?>  
14             <li class="list-group-item"><a href="#"><?php echo $value['topic_title'];?></a></li>  
15             <?php } ?>  
16         </ul>  
17     </div>  
18 </body>  
19 </html>  

test.php代码如下:

 1 <?php
 2 //1、连接数据库,然后从数据库里面获取数据  
 3 //2、把获取到的数据填充到模板文件里面  
 4 //3、需要把动态的页面转化为静态页面,生成纯静态化文件 
 5 header("content-type:text/html;charset=utf-8");
 6 if(is_file('index.shtml')&&(time()-filemtime('index.shtml'))<300){
 7     require_once('index.shtml');
 8 }else{
 9     require_once('db.php'); 
10     $connect = DB::getInstance()->connect();
11     $sql = 'select * from ask_topic';
12     $result=mysql_query($sql,$connect);
13     $news=array();
14     while($new=mysql_fetch_assoc($result)){
15         $news[]=$new;
16     }
17    //开启缓存区    
18    ob_start();  
19    //引入模板文件 
20    require_once('template.php');//动态文件。template.php界面同样进过缓冲区
21    file_put_contents('index.shtml', ob_get_contents());// 当我们第一次访问 index.php时,服务器将为我们生成一个静态文件index.shtml。
22 }

 静态化页面中如何想加载动态的内容如何处理?

Ajax技术:jquery中ajax请求方式$.ajax()

实现步骤:编写接口-》ajax请求接口操作

接口数据:1.获取数据

                  2. 把我们获取到的数据组装成接口数据通信

接口hot.php代码如下:

 1 <?php
 2 header("content-type:text/html;charset=utf-8");
 3 require_once 'db.php'; //引用连接数据库
 4 $connect = DB::getInstance()->connect();
 5 $sql = 'select `topic_desc` from ask_topic';
 6 $result=mysql_query($sql,$connect);
 7 $hots=array();
 8 while($hot=mysql_fetch_assoc($result)){
 9     $hots[]=$hot;
10 }
11 return show(200,'success',$hots);
12 function show($code=0,$message="error",$data=array()){
13   $result=array(
14    'code'=>$code,
15    'message'=>$message,
16   'data'=>$data,
17   );
18 echo json_encode($result);
19 }

再次修改template.php 增加ajax动态调用部分。修改之后的代码如下:

 1 <!DOCTYPE html>  
 2 <html lang="en">  
 3 <head>  
 4     <meta charset="UTF-8">  
 5     <title>新闻中心</title>  
 6       <script type="text/javascript" src="http://localhost/2019/jquery.min.js"></script> 
 7 </head>  
 8 <body>  
 9     
10     <div class="container">  
11         <h3>新闻列表</h3>  
12         <ul class="list-group">  
13             <?php foreach ($news as $key => $value) { ?>  
14             <li class="list-group-item"><a href="#"><?php echo $value['topic_title'];?></a></li>  
15             <?php } ?>  
16         </ul>  
17     </div>  
18 
19          <h3>动态调用部分内容</h3>  
20         <ul class="list-group" id="hot">  
21             
22         </ul>  
23  <script type="text/javascript">
24      $.ajax({
25         url:"http://localhost/2019/hot.php",
26         dataType:'json',
27         type:'post',
28         success:function(result){
29            if(result.code==200){
30              html='';
31             $.each(result.data,function(key,value){
32            
33     html+='<li><a href="/">'+value.topic_desc+'</a></li>';
34         });
35             $("#hot").html(html);
36             }
37         }
38      });
39  </script>  
40 
41 
42 </body>  
43 </html>  

访问test.php的时候 就可以显示效果

posted @ 2018-11-29 14:37  L1230205  阅读(1388)  评论(0编辑  收藏  举报