note 35622 added to function.set-error-handler
| From: | smp at rn2 dot php dot net | Date: | Mon, 08 Sep 2003 19:28:31 +0000 |
| Subject: | note 35622 added to function.set-error-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55858@lists.php.net to get a copy of this message | ||
Here is an example of catching ALL errors from php including parse and compile. This example comes
from a web page referenced in another post (http://www.webkreator.com/cms/view.php/1692.html ). My
example doesn't require any .htaccess editing at all:
function fatal_error_handler($buffer) {
if (ereg("(error</b>:)(.+)(<br)", $buffer, $regs) ) {
$err = preg_replace("/<.*?>/","",$regs[2]);
error_log($err);
return "ERROR CAUGHT check log file";
}
return $buffer;
}
function handle_error ($errno, $errstr, $errfile, $errline)
{
error_log("$errstr in $errfile on line $errline");
if($errno == FATAL || $errno == ERROR){
ob_end_flush();
echo "ERROR CAUGHT check log file";
exit(0);
}
}
ob_start("fatal_error_handler");
set_error_handler("handle_error");
//causes a warning
preg_replace();
//would normally cause a fatal error, but instead our output handler will be called allowing us to
handle the error.
somefunction();
ob_end_flush();
The trick is to buffer the output and setup your error handling and output handling first thing.
When a fatal error occurs, the output from php will be sent to the function fatal_error_handler
giving you one last chance to handle it.
----
Manual Page -- http://www.php.net/manual/en/function.set-error-handler.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35622
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35622&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35622&report=yes