phpcachingob-start

PHP Codes After Caching [How to]


I have a problem in my php codes. I'am using a simple cache system with this function.

// Cache Function
function cachestart($min){
    global $here;
    global $cachefile;
    $cachedosyasi = 'cache/' . 'file' . md5($here);
    if(file_exists($cachefile) && (time() - $min*60 < filemtime($cachefile))){
    include($cachefile);
    die;
    }else{
        ob_start();
        global $cache;
        $cache = 1;
    }
}

function cachefinish(){
    global $cache;
    global $cachefile;
    if(@$cache){
        $ch = fopen($cachefile, 'w');
        fwrite($ch, ob_get_contents());
        fclose($ch);
        ob_end_flush(); 
    }
}

I'am trying that:

-- Some Queries (Controls, is user logged etc. )
<< Start Caching >> (With cachestart() )
-- Some Queries (Show entries in database. )
<< Stop Caching >> (With cachefinish() )
-- Some Queries (Comment box)

But i can't this because of "die". I don't have an idea about it.

Thanks for help!


Solution

  • So this isn't perfect, but using your existing code; just return true or false and have your application make decisions based on that:

    function cachestart($min){
        global $cachefile;
        if(file_exists($cachefile) && (time() - $min*60 < filemtime($cachefile))){
            include($cachefile);
            //we are not caching
            return false;
        } else {
            ob_start();
            //we are caching
            return true;
        }
    }
    

    Make a decision at the start of your application:

    if(!cachestart(60)) {
        //page loaded from cache
        //maybe do some queries for comment box
        die(); //or call cachefinish() with a die() in there
    }
    
    //main application code