note 35622 deleted from function.set-error-handler by danbrown

From: Date: Mon, 18 Oct 2010 16:16:48 +0000
Subject: note 35622 deleted from function.set-error-handler by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173254@lists.php.net to get a copy of this message
Note Submitter: smp at ncoastsoft dot com ---- 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.

« previous php.notes (#173254) next »