Detecting PHP output buffer leaks

If you want to check for "ob_start()" leaks or incorrectly closed buffers, add the check at the end of each file you wish to check.

If running PHP in command line mode, by default there are no buffer levels.
However, when the request is from a browser, the default buffer level is 1.

So, at the end of the script, add the following.
if (ob_get_length() && ob_get_level() > 1) {
error_log("ERROR: Output buffering leakage detected: ({$_SERVER['REQUEST_METHOD']}) {$_SERVER['REQUEST_URI']}");
error_log("ERROR: Output buffering args:\n" . print_r($_SERVER['argv'], true));
}
The "ob_get_length()" checks if there is any data in the current buffer.
Typically this would be sufficient in command line, but since a browser request has one level of buffer set, we need to also check "ob_get_level()".
 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog