【问题标题】:ob_start() output buffering doesn't work with ob_gzhandler compressionob_start() 输出缓冲不适用于 ob_gzhandler 压缩
【发布时间】:2012-03-29 15:47:43
【问题描述】:

我对使用 ob_start 和 ob_start("ob_gzhandler") 函数感到困惑。 我有一个测试 php 脚本,我试图在其中实现一个简单的缓存系统。这是脚本:

<?php

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 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
echo '<meta name="description" content="'.$description.'" />';
echo '<meta name="keywords" content="'.$keywords.'" />';
echo '<meta name="title" content="'.$title.'" />';
echo '<link rel="icon" href="'.filemtime("favicon.ico").'/favicon.ico" type="image/x-icon" />';
echo '<link rel="shortcut icon" href="'.filemtime("favicon.ico").'/favicon.ico" type="image/x-icon"/>';
echo '<title>'.$title.' </title>';
echo '<link rel="stylesheet" type="text/css" href="/css/'.filemtime("css/style.php").'/style.php" media="screen" />';
echo '</head>';
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 '<body>';
    echo '<p>this is the body of html doc!</p>';
    echo '<p>this part of page was cached in buffer</p>';
    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 '<p>this is a footer section!</p>';
    echo '</body></html>';
    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 '<p>usual output of data...'.date('Y-m-d H:i:s');
?>

脚本输出这些行:

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

所有三个缓存文件都存在于缓存目录中,一切似乎都正常。但在我在脚本中实现输出缓冲之前,我有

ob_start("ob_gzhandler",9);

而不是

ob_start();

在脚本的开头。

所以我在我的测试页面上使用了 gzip 压缩。现在如果我尝试改变 ob_start() 到 ob_start("ob_gzhandler",9) 我的浏览器和空白窗口出现内容类型错误。为什么我不能在我的页面上不仅使用输出缓冲,还要使用 gzip 压缩?有没有办法像这样使用 gzip 压缩内容来组织输出缓冲缓存?

希望有人知道答案!谢谢!

【问题讨论】:

    标签: php caching gzip output-buffering


    【解决方案1】:

    PHP 中使用 ob_gzhandler 的错误,必须使用 ob_flush() 或 ob_end_flush() 而不是 ob_end_clean() 或 ob_get_clean()

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

    我以这样的方式开始页面:

    if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
    
        ob_start("ob_gzhandler");
    
    }else{
    
        ob_start();
    
    }
    

    我用这个结束页面:

    ob_end_flush();
    

    我包装了我想缓存的元素:

    ob_start(); and ob_get_clean();
    

    【讨论】:

    • 我从命令行使用 curl 来找出问题所在,并弄清楚如何开始解决问题。命令行中的 Curl 是确定浏览器的真实输出的宝贵工具。过去用它解决了许多编码问题和其他问题。
    • 哦,现在我明白了!感谢您对问题的解释!那么我会接受您关于 ob_end_flush 的建议并感谢您包装缓存!
    猜你喜欢
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多