PHP伪静态简单实现(八)

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值