Heim  >  Fragen und Antworten  >  Hauptteil

php - 静态页面怎么实现,就是把数据写入到html页面在缓存起来?

static.php 文件

<?php

$file  = "static.html";
$ctime =filectime($file);
$expr = 3600*24*10;//静态文件有效期,十天
if(file_exists($file)) {
    if($ctime+$expr> time()){
        echo file_get_contents($file);
    }else{
        unlink($file);//删除过期的静态页文件
         ob_start();
         //从数据库取出数据写入 static.html 
         
        $content = ob_get_contents();
        file_put_contents($file,$content);//写入内容到对应静态文件中
        ob_end_flush();
    }
    
 }else{
           ob_start();
         //从数据库取出数据写入 static.html 
        
        $content = ob_get_contents();
        file_put_contents($file,$content);//写入内容到对应静态文件中
        ob_end_flush();
    
 }

?>

static.html 页面

<html>

<head>  
<title>静态页面</title>  
<style type="text/css"> 
</head>
<body>
    <h1>数据库的数据1</h1> 
    <h2>数据库的数据2</h2>
    <h3>数据库的数据3</h3>
</p>  
</body>
</html>  

问题:就是当我第一次执行static.php的时候,是要从数据库查询数据,然后写入到static.html页面,同时显示static.html页面,这个时候static.html也成了一个静态页面了。
现在我不知道在

        ob_start();
         //从数据库取出数据写入 static.html 
         //同时显示 static.html
        $content = ob_get_contents();
        之间怎么处理,就是怎么把数据库的数据写入到static.html 里去,同时显示出来。
        
阿神阿神2723 Tage vor232

Antworte allen(1)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-04-11 09:43:59

    直接在ob_start() 后面 include你的模板文件, 跟用混编一样的。。
    include完成以后,直接echo $content; 就是显示出来

    Antwort
    0
  • StornierenAntwort