2013-04-26 88 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

相關問題