donglun4521 2013-04-26 12:07
浏览 25
已采纳

包含文件并替换它[关闭]

I have a caching function and got HTML files.

Problem is I want to include my file into my page.

Example :

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

So my cache's function is so simple like...

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

Problem is ...

include($cachefile);
exit;

It's stop rendering after included. I tried to remove exit so I got 2 multiple contents.

Any ?

  • 写回答

2条回答 默认 最新

  • doufu7835 2013-04-26 12:46
    关注

    Im done now :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);
            }
        }
    }
    

    So do like this...

    <h2>Before</h2>
    <?php
    cache('start',10);
    // content....
    cache('end',10);
    ?>
    <footer>After</footer>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀