<?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);
}
}