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') //判断给定文件名是否是一个目录