21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
ob_start(); //打开输出缓冲区
echo 'Hello World';
<?php
ob_start();
echo 'Hello World';
/**
* ob_get_contents 能返回把内部缓存的数据
* 这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 FALSE
* 这里把ob_get_contents 返回的值就可以赋值给 $content
*/
$content = ob_get_contents();
/**
* 这里我们可以使用ob_end_clean
* 这个函数不会输出内部缓冲区的内容而是把它删除!
*/
ob_end_clean();
//这样你是不是发现没有输出任何东西呢?
//我们上传明明输出了 Hello World 的呀!
//所以说ob_end_clean(); 就是把缓存里的内容清除 并停止缓存
//如果不行你把最上面的缓存开启那行删掉看看
<?php
ob_start(); //开启缓存
echo 'Hello World';
$content = ob_get_contents(); //把内部缓存赋值给$content
ob_end_clean();//清除缓存并关闭
echo $content; //是不是有看到效果了?
<?php
ob_start(); //开启缓存
echo 'Hello World';
$content = ob_get_contents(); //把内部缓存赋值给$content
ob_end_clean();//清除缓存并关闭
//然后我们把$content的值写成文档怎么样? GO!
$fp = fopen('html.html', "w");
fwrite($fp, $content);
fclose($fp);
//看看你的文档里是不是多了一个页面
//好生成了今天页面了怎么办呢
<?php
ob_start();//开启缓存
/**
* 这里我们就开始真正的生成静态页面了
*/
?>
<?php
for ($i = 0; $i < 20; $i++)
{
echo "$i\n\r<br />";
}
//这里就把for循环里的数据缓存起来了 因为上面我们ob_start了的
?>
<?php
$content = ob_get_contents();
$fp = fopen('test5.html', "w");
fwrite($fp, $content);
fclose($fp);
ob_end_clean();
//ok
<?php
//以为这里程序里我们需要使用时间 所以我们需要把时间改一下 改成亚洲/上海的时间
date_default_timezone_set ("Asia/Shanghai");
//这里我们就和test5一样 但是有点区别
//这里换成r 以 read 只读方式打开,将文件指针指向文件头
$fp = @fopen('test6.html', "r");
//这里有要判断是否有这个test6.html文件了
if ($fp)
{
//如果有就 肯定就是以读当方式打开咯 然后我们需要一样他的东西就是他的这个文件生成时间
//文件生成时间 函数:filectime
//获取文件创建时间 ,获取的时间是一个时间戳
$fileCreateTime = filectime('test6.html');
//获取文件的修改时间
$fileMakeTime = filemtime('test6.html');
//这个可以看到文件的创建时间
echo '文件的创建时间:'.date("Y-m-d G:i:s T", $fileCreateTime);
echo '<br />';
echo '文件的修改时间:'.date("Y-m-d G:i:s T", $fileMakeTime);
echo '<br />';
echo '当前时间:'.date("Y-m-d G:i:s T", time());
}
else
{
//如果没这个文件就创建这个文件咯
//以写的方式打开并创建test6.html这个文件
$fp = @fopen('test6.html', "w");
}
//这就就是想做一个文件的写和读操作 然后要要获取文件的创建时间和修改时间
<?php
//开启缓存
ob_start();
//修改时间差
date_default_timezone_set ("Asia/Shanghai");
//获取当前文件名 得到:/test7.php
$selfName = $_SERVER['PHP_SELF'];
//用正则替换前面的“/”和后面的“.php” 得到 test7
$selfName = preg_replace('/\/|(.php)/', '' ,$selfName);
//然后在他文件后面加“.html” 得到 test7.html
$selfName .= '.html';
//以读的方式打开文件
$fp = @fopen($selfName, "r");
if ($fp)
{
//文件修改时间
$fileMakeTime = filemtime($selfName);
/**
* 判断是否小于当前时间的$differ秒
* 如果小于的话证明这个文件已经过期了
* 因为我们的PHP是动态的玩意你在后台修改添加或删除了内容的话 这样的话别人就看不到你更新的内容了
* 所以就要重新生成html文档
* 如果大于的话直接给访问者看HTML文档就是了
*/
$differ = 60*1;//以为时间戳也是以秒来算了所以这里就似乎1分钟的时间了
if ($fileMakeTime > (time() - $differ))
{
//表HTML文档没有过期就给用户看这个HTML文档 用header来跳转吧
header("location:$selfName");
//然后后面的程序也没有必要运行下去了
echo '跳转吧';
fclose($fp);
exit;
}
}
//这后面就是文件过期 就需要重新生成咯
for ($i = 0; $i < 100 ; $i++)
{
echo "$i\n\r<br />";
}
//把缓存的文件放当$content里去把
$content = ob_get_contents();
//结束缓存并输出缓存内的内容ob_end_flush()就是这功能
ob_end_flush();
//最后把缓存里的东西写成一个html静态的文档吧
$fp = @fopen($selfName, "w");
fwrite($fp, $content);
fclose($fp);
<?php
class FalseStaticHtml
{
//静态页面的文件名和后缀
private $file;
//打开的静态文件
private $fp;
public function __construct()
{
//开启缓存
ob_start();
}
/**
* 如果time大于的话直接给访问者看HTML文档就是了
* 并停止程序的运行 然后跳转到静态页面里去
* GoToHTML(string fileName = null, string suffix = 'html', string path = null, uint time = 60)
* return void
* string fileName 默认为:当前php文件名
* string suffix 默认为:html
* string path 默认为:当前php所在目录
* uint time 默认为:1分钟
*/
public function GoToHTML($fileName = null, $suffix = 'html', $path = null, $time = 60)
{
$this->setFile($fileName, $suffix , $path);
$this->fp = @fopen($this->file , "r");
if ($this->fp)
{
if (filemtime($this->file) > (time() - $time))
{
header("location:$this->file");
exit;
}
}
}
/**
* 设置静态文件的文件的名称
* setFile($fileName = null, suffix = 'html', path = null)
* return void
* 可以都为空,如果fileName为空,那么文件名就位当前文件名
* 如果后缀名为空,默认为html
* 如果path为空
* 默认为当前php文件所在的目录
*/
private function setFile($fileName = null, $suffix = 'html', $path = null)
{
if ($fileName == "" || $fileName == null)
{
$this->file = preg_replace('/\.php/', '' , end(explode('/', $_SERVER['PHP_SELF'])));
$this->file .= '.'.$suffix;
}
else
{
$this->file = $fileName.'.'.$suffix;
}
$this->file = $path.$this->file;
}
/**
* 生成HTML文档
*/
public function WriteHTML()
{
$this->fp = fopen($this->file, "w");
fwrite($this->fp, ob_get_contents());
ob_end_flush();
}
public function __destruct()
{
fclose($this->fp);
}
}
<?php
require_once('FalseStaticHtml.php');
$html = new FalseStaticHtml();
/**
* 如果time大于的话直接给访问者看HTML文档就是了
* 并停止程序的运行 然后跳转到静态页面里去
* GoToHTML(string fileName = null, string suffix = 'html', string path = null, uint time = 60)
* return void
* string fileName 默认为:当前php文件名
* string suffix 默认为:html
* string path 默认为:当前php所在目录
* uint time 默认为:1分钟
* 还需要注意的是 path不能为觉得磁盘路径如:D:\www\Blog\Library
* 只能为如:'/' 或'../' 或 '../path/'
*/
$html->GoToHTML('name', 'html', '../', 10);
echo date('h-i-s', time());
$html->WriteHTML();