Voting

: five plus three?
(Example: nine)

The Note You're Voting On

jazfresh at SPAM-JAVELIN dot hotmail dot com
21 years ago
In the set_error_handler notes, there is a technique described for capturing all errors (even parse errors) before they are displayed the user, using a special error handler and output handler. If this output handler detects a fatal error in the output buffer, it's captured and dealt with before it can be displayed to the user. If no error was detected, then output buffer is displayed verbatim (i.e. without being compressed).

If you are using this method, you can still take advantage of ob_gzhandler's compression. However, you MUST specify a mode argument (I'm using 4.2.2 on RedHat9). The mode value affects which headers are automatically added (Content-Encoding, etc). A value of '5' worked for me. '0' or discarding the argument produces a blank screen under Mozilla.

<?php

function my_output_handler(&$buffer) {
// Detect errors in the output
if(ereg("(error</b>:)(.+) in <b>(.+)</b> on line <b>(.+)</b>", $buffer, $regs)) {
my_error_handler(E_ERROR, $regs[2], $regs[3], $regs[4]);
// ...
// ... Insert your error handling here ...
// ...
return 'An internal error occurred.';
} else {
// The page rendered without any errors, so compress
// and output.
return ob_gzhandler($buffer, 5);
}
}
?>

<< Back to user notes page

To Top