PHP的buffer缓冲机制实现静态页面

PHP的buffer缓冲机制实现纯静态页面

php.ini文件

    output_buffering = on/off  //打开/关闭php的buffer(文件缓冲机制) 不懂版本默认值不同

php中关于buffer的常用函数

ob_start() 开始输出缓冲, 这时PHP停止输出, 在这以后的输出都被转到一个内部的缓冲里.
ob_get_contents() 这个函数返回内部缓冲的内容. 这就等于把这些输出都变成了字符串.
ob_clean()   清空输出缓冲
ob_get_clean 得到当前的缓冲区内容,并且删除当前输出的缓冲区

ob_get_ length() 返回内部缓冲的长度.
ob_end_flush() 结束输出缓冲, 并输出缓冲里的内容. 在这以后的输出都是正常输出.
ob_end_clean() 结束输出缓冲, 并扔掉缓冲里的内容.

代码流程示例

php代码

<?php
/*-----------------生成缓存文件-----------------
include('./DatabaseOperate.class.php');
$obj = new DatabaseOperate(0,0,0,0);
$sql = 'select * from vr_comment';
$list = $obj -> getAllArray($sql);
echo "<pre>";
    print_r($list);

ob_start();
require_once('./test.html');

if(file_put_contents('./test1/index.shtml',ob_get_clean())){ 
    echo "success";
}else{ 
    echo 'file';
}
------------------------------------------------*/
/*------------------设定时间刷新------------------
if(is_file('./test1/index.shtml') && (time()-filemtime('./test1/index.shtml')) < 5 ){ 
    echo "haved</br>";
    require_once('./test1/index.shtml');

}else{ 
    echo "not</br>";
    include('./DatabaseOperate.class.php');
    $obj = new DatabaseOperate(0,0,0,0);
    $sql = 'select * from vr_comment';
    $list = $obj -> getAllArray($sql);
    ob_start();
    require_once('./test.html');
    file_put_contents('./test1/index.shtml',ob_get_contents());
}
--------------------------------------------------*/
?>

html页面代码

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <meta charset="utf-8"/>
</head>
<body>
    <?php foreach($list as $key => $val):?>
    <div style="color:red;"><?php echo $val['content'];?></div>
    <?php endforeach;?>
</body>
</html>

归纳总结

1,设定刷新的问题
        手动刷新
        设定时间  time()-filemtime('./test1/index.shtml')
        linux系统中用crontab定时刷新脚本  */5 * * * * * php/……path……/index.php

2,filemtime('file')         //文件的最后修改时间
   is_file('file')          //判断给定文件名是否为一个正常的文件。
                              存在且正常,true。不存在或者不正常,file
   is_dir('file')           //判断给定文件名是否是一个目录
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值