Friday 24 June 2016

php - ob_start() output buffering doesn't work with ob_gzhandler compression



I'm confused about using ob_start and ob_start("ob_gzhandler") functions.
I have a test php script in which I'm trying to implement a simple caching system. Here is the script:





ob_start();

function writeCache($content, $filename) {
$fp = fopen($filename, 'w');
fwrite($fp, $content);
fclose($fp);
}


function readCache($filename, $expiry) {
if (file_exists($filename)) {
if ((time() - $expiry) > filemtime($filename))
return FALSE;
$cache = file($filename);
return implode('', $cache);
}
return FALSE;
}


$headfile='cache/head.cache';
$headtime=86400; //1 day cache
// check if a valid head cache exists
if (!$head = readCache($headfile,$headtime)) {

$title='testing output buffering in caching';
$description='this page is using ob_start for caching';
$keywords='ob_start,output,buffering';


echo '';
echo '';
echo '';
echo '';
echo '';
echo '';
echo ''.$title.' ';
echo '';
echo '';
echo 'head cached at '.date('Y-m-d H:i:s');

$head = ob_get_contents();
ob_clean();
writeCache($head,$headfile);

}

//body cache
$bodyfile='cache/body.cache';
$bodytime=86400; //1 day cache


// check if a valid body cache exists
if (!$body = readCache($bodyfile,$bodytime)) {

echo '';
echo '

this is the body of html doc!

';
echo '

this part of page was cached in buffer

';
echo 'body cached at '.date('Y-m-d H:i:s');
$body = ob_get_contents();
ob_clean();
writeCache($body,$bodyfile);

}

//footer cache
$footerfile='cache/footer.cache';
$footertime=86400; //1 day cache

// check if a valid footer cache exists
if (!$footer = readCache($footerfile,$footertime)) {

echo '

this is a footer section!

';

echo '';
echo 'footer cached at '.date('Y-m-d H:i:s');
$footer = ob_get_contents();
ob_clean();
writeCache($footer,$footerfile);
}
ob_end_clean();
echo $head.$body.$footer;
echo '

usual output of data...'.date('Y-m-d H:i:s');
?>



Script outputs these lines:



head cached at 2012-03-29 19:34:36

this is the body of html doc!

this part of page was cached in buffer
body cached at 2012-03-29 19:34:36


this is a footer section!
footer cached at 2012-03-29 19:34:36

usual output of data...2012-03-29 20:13:59


All three cache files are present in cache directory and everything seem to work ok. But before I implemented output buffering into the script I had



ob_start("ob_gzhandler",9);



instead of



ob_start();


at the beginning of script.



So i used gzip compression on my test page. Now if I try to change ob_start()

to ob_start("ob_gzhandler",9) I got content type error in my browser and blanc window. Why can't I use not only output buffering, but gzip compression on my page too? Is there a way to organize output buffering caching like this using gzip compression of content too?



Hope someone knows the answer! Thanks!


Answer



bug in PHP on use of ob_gzhandler, must use ob_flush() or ob_end_flush() with it instead of ob_end_clean() or ob_get_clean()



http://bugs.php.net/bug.php?id=34071



I start the page with something like this:




if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){

ob_start("ob_gzhandler");

}else{

ob_start();

}



I end the page with this:



ob_end_flush();


I wrap elements I wanna cache in:



ob_start(); and ob_get_clean();


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...