简体   繁体   English

PHP输出缓冲区

[英]PHP Output buffer

My own PHP framework parses files like this to grab the content: 我自己的PHP框架解析这样的文件以获取内容:

ob_start();
include($file);
$content = ob_get_clean();

However, now I am working on an own error_handler to display an error page if any error occured even when it's happning in a template parsed like the code piece demonstrates above. 但是,现在我正在使用自己的error_handler来显示错误页面,即使发生任何错误,即使该错误正发生在如上述代码段所示的已解析模板中也是如此。

The funny thing is the content of the included template is printed to the browser when code execution dies due to an error for example. 有趣的是,例如由于错误而导致代码执行终止时,所包含模板的内容会打印到浏览器中。 That of course makes my error-page looks very bad. 当然,这会使我的错误页面看起来非常糟糕。

Shouldn't the opening of an output buffer prevent printing the content? 打开输出缓冲区是否应该阻止打印内容?

Especially because ob_get_clean() should clear the content after parsing it. 特别是因为ob_get_clean()在解析内容后应清除内容。

Why is this happening? 为什么会这样呢?

If I don't run into an error the website is working as intended. 如果我没有遇到错误,则说明网站正在按预期运行。

How can I fix this problem? 我该如何解决这个问题?

I think I had the same problem. 我想我也有同样的问题。 Try using ob_clean(): 尝试使用ob_clean():

try {
    $app = new App($some_config);
    $app->run();
} 
catch (Exception $e) {
    ob_clean();
    // Display your error page.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM