2013-04-26 62 views
0

我有一个缓存功能并获得了HTML文件。包含一个文件并替换它

问题是我想将我的文件加入到我的页面。

例子:

<h2>Before</h2> 
<?php 
cache('start'); 
// content.... 
cache('end'); 
?> 
<footer>After</footer> 

所以我的缓存的功能是如此简单的像......

function cache($a,$min=null) { 
    global $cachefile; 
    $cache_path = "/cached/"; 
    $file_name = basename(rtrim($_SERVER["REQUEST_URI"],'/')); 
    $file_path = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
    $cachefile = $cache_path.sha1($file_path).'.cache'; 
    if($a == 'start'){ 
     $lifetime = $min * 60; 
      if(file_exists($cachefile)&&time()-$lifetime<filemtime($cachefile)){ 
       include($cachefile); 
       exit; 
      } 
      ob_start(); 
    } 
    if($a == 'end'){$fp=fopen($cachefile,'w');fwrite($fp,ob_get_contents());fclose($fp);ob_end_flush();} 
} 

问题是...

include($cachefile); 
exit; 

它停止渲染包括在内。我试图删除exit,所以我得到2多个内容。

有没有?

+0

只是把你的函数'cache'在全球utils的文件,包括一次 – Adidi 2013-04-26 12:11:01

+0

你得到任何错误或页面空白 – 2013-04-26 12:18:46

+0

没有任何错误,我只想替换'cache('start');'和'cache('stop')之间的所有内容;'@NanheKumar – l2aelba 2013-04-26 12:22:08

回答

0

林现在做:d

function cache($a,$min=null) { 
    global $cachefile; 
    $cache_path = get_template_directory()."/cached/"; 
    $file_name = basename(rtrim($_SERVER["REQUEST_URI"],'/')); 
    $file_path = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 
    $cachefile = $cache_path.sha1($file_path).'.cache'; 
    $lifetime = $min * 60; 
    if($a == 'start'){ 
      if(file_exists($cachefile)&&time()-$lifetime<filemtime($cachefile)){ 
       include_once($cachefile); 
      } 
      ob_start(); 
    } 
    if($a == 'end'){ 
     if(file_exists($cachefile)&&time()-$lifetime<filemtime($cachefile)){ 
      ob_end_clean(); 
     } else { 
      $fp=fopen($cachefile,'w'); 
      fwrite($fp,ob_get_contents()); 
      fclose($fp); 
     } 
    } 
} 

所以做这样的...

<h2>Before</h2> 
<?php 
cache('start',10); 
// content.... 
cache('end',10); 
?> 
<footer>After</footer> 
1

您可以使用include_once。它会迫使包括只运行一次。那是你在找什么?

+0

让我试试看,只需一分钟 – l2aelba 2013-04-26 12:12:51

+0

nope,做se'nt work :( – l2aelba 2013-04-26 12:13:41

+0

'include_once($ cachefile);退出;'对吗? – l2aelba 2013-04-26 12:14:44

相关问题