Documentation on set_error_handler
set_error_handler = Sets a user-defined error handler function
Sets a user function (error_handler) to handle errors in a script.
This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions (using trigger_error()). It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator. Also note that it is your responsibility to die() if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error. The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called. If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.
Usage, params, and more on set_error_handler
mixed set_error_handler ( callable $error_handler
[, int $error_types
= E_ALL | E_STRICT ] )
error_handler
A callback with the following signature. NULL
may be passed instead, to reset this handler to its default state. Instead of a function name, an array containing an object reference and a method name can also be supplied. bool handler ( int $errno
, string $errstr
[, string $errfile
[, int $errline
[, array $errcontext
]]] ) errno
The first parameter, errno
, contains the level of the error raised, as an integer. errstr
The second parameter, errstr
, contains the error message, as a string. errfile
The third parameter is optional, errfile
, which contains the filename that the error was raised in, as a string. errline
The fourth parameter is optional, errline
, which contains the line number the error was raised at, as an integer. errcontext
The fifth parameter is optional, errcontext
, which is an array that points to the active symbol table at the point the error occurred. In other words, errcontext
will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context. If the function returns FALSE
then the normal error handler continues. error_types
Can be used to mask the triggering of the error_handler
function just like the error_reporting ini setting controls which errors are shown. Without this mask set the error_handler
will be called for every error regardless to the setting of the error_reporting setting.
Returns a string containing the previously defined error handler (if any). If the built-in error handler is used NULL
is returned. NULL
is also returned in case of an error such as an invalid callback. If the previous error handler was a class method, this function will return an indexed array with the class and the method name.
Notes and warnings on set_error_handler
Basic example of how to use: set_error_handler
Example #1 Error handling with set_error_handler() and trigger_error()
The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function:
<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function scale_by_log($vect, $scale)
{
if (!is_numeric($scale) || $scale <= 0) {
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
}
if (!is_array($vect)) {
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
return null;
}
$temp = array();
foreach($vect as $pos => $value) {
if (!is_numeric($value)) {
trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
$value = 0;
}
$temp[$pos] = log($scale) * $value;
}
return $temp;
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);
// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);
// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL
// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>
The above example will output something similar to:
vector a Array ( [0] => 2 [1] => 3 [2] => foo [3] => 5.5 [4] => 43.3 [5] => 21.11 ) ---- vector b - a notice (b = log(PI) * a) <b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br /> Array ( [0] => 2.2894597716988 [1] => 3.4341896575482 [2] => 0 [3] => 6.2960143721717 [4] => 49.566804057279 [5] => 24.165247890281 ) ---- vector c - a warning <b>My WARNING</b> [512] Incorrect input vector, array of values expected<br /> NULL ---- vector d - fatal error <b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br /> Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br /> Aborting...<br />
Other code examples of set_error_handler being used
<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function scale_by_log($vect, $scale)
{
if (!is_numeric($scale) || $scale <= 0) {
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
}
if (!is_array($vect)) {
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
return null;
}
$temp = array();
foreach($vect as $pos => $value) {
if (!is_numeric($value)) {
trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
$value = 0;
}
$temp[$pos] = log($scale) * $value;
}
return $temp;
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);
// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);
// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL
// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>
By this function alone you can not catch fatal errors, there is a simple work around. Below is part of my error.php file which handles errors and exceptions in the application. Before someone complains I'll add that I do not care that I am using globals, this file is part of my mini framework and without the 'config' variable the application would crash anyways.
<?php
/**
* Error handler, passes flow over the exception logger with new ErrorException.
*/
function log_error( $num, $str, $file, $line, $context = null )
{
log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
}
/**
* Uncaught exception handler.
*/
function log_exception( Exception $e )
{
global $config;
if ( $config["debug"] == true )
{
print "<div style='text-align: center;'>";
print "<h2 style='color: rgb(190, 50, 50);'>Exception Occured:</h2>";
print "<table style='width: 800px; display: inline-block;'>";
print "<tr style='background-color:rgb(230,230,230);'><th style='width: 80px;'>Type</th><td>" . get_class( $e ) . "</td></tr>";
print "<tr style='background-color:rgb(240,240,240);'><th>Message</th><td>{$e->getMessage()}</td></tr>";
print "<tr style='background-color:rgb(230,230,230);'><th>File</th><td>{$e->getFile()}</td></tr>";
print "<tr style='background-color:rgb(240,240,240);'><th>Line</th><td>{$e->getLine()}</td></tr>";
print "</table></div>";
}
else
{
$message = "Type: " . get_class( $e ) . "; Message: {$e->getMessage()}; File: {$e->getFile()}; Line: {$e->getLine()};";
file_put_contents( $config["app_dir"] . "/tmp/logs/exceptions.log", $message . PHP_EOL, FILE_APPEND );
header( "Location: {$config["error_page"]}" );
}
exit();
}
/**
* Checks for a fatal error, work around for set_error_handler not working on fatal errors.
*/
function check_for_fatal()
{
$error = error_get_last();
if ( $error["type"] == E_ERROR )
log_error( $error["type"], $error["message"], $error["file"], $error["line"] );
}
register_shutdown_function( "check_for_fatal" );
set_error_handler( "log_error" );
set_exception_handler( "log_exception" );
ini_set( "display_errors", "off" );
error_reporting( E_ALL );
<?php
/**
* throw exceptions based on E_* error types
*/
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
// error was suppressed with the @-operator
if (0 === error_reporting()) { return false;}
switch($err_severity)
{
case E_ERROR: throw new ErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_WARNING: throw new WarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_PARSE: throw new ParseException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_NOTICE: throw new NoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_CORE_ERROR: throw new CoreErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_CORE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_COMPILE_ERROR: throw new CompileErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_COMPILE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_ERROR: throw new UserErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_WARNING: throw new UserWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_NOTICE: throw new UserNoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_STRICT: throw new StrictException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_RECOVERABLE_ERROR: throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_DEPRECATED: throw new DeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
case E_USER_DEPRECATED: throw new UserDeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
}
});
class WarningException extends ErrorException {}
class ParseException extends ErrorException {}
class NoticeException extends ErrorException {}
class CoreErrorException extends ErrorException {}
class CoreWarningException extends ErrorException {}
class CompileErrorException extends ErrorException {}
class CompileWarningException extends ErrorException {}
class UserErrorException extends ErrorException {}
class UserWarningException extends ErrorException {}
class UserNoticeException extends ErrorException {}
class StrictException extends ErrorException {}
class RecoverableErrorException extends ErrorException {}
class DeprecatedException extends ErrorException {}
class UserDeprecatedException extends ErrorException {}
<?php
/**
* Used for logging all php notices,warings and etc in a file when error reporting
* is set and display_errors is off
* @uses used in prod env for logging all type of error of php code in a file for further debugging
* and code performance
* @author Aditya Mehrotra<aditycse@gmail.com>
*/
error_reporting(E_ALL);
ini_set("display_errors", "off");
define('ERROR_LOG_FILE', '/var/www/error.log');
/**
* Custom error handler
* @param integer $code
* @param string $description
* @param string $file
* @param interger $line
* @param mixed $context
* @return boolean
*/
function handleError($code, $description, $file = null, $line = null, $context = null) {
$displayErrors = ini_get("display_errors");
$displayErrors = strtolower($displayErrors);
if (error_reporting() === 0 || $displayErrors === "on") {
return false;
}
list($error, $log) = mapErrorCode($code);
$data = array(
'level' => $log,
'code' => $code,
'error' => $error,
'description' => $description,
'file' => $file,
'line' => $line,
'context' => $context,
'path' => $file,
'message' => $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']'
);
return fileLog($data);
}
/**
* This method is used to write data in file
* @param mixed $logData
* @param string $fileName
* @return boolean
*/
function fileLog($logData, $fileName = ERROR_LOG_FILE) {
$fh = fopen($fileName, 'a+');
if (is_array($logData)) {
$logData = print_r($logData, 1);
}
$status = fwrite($fh, $logData);
fclose($fh);
return ($status) ? true : false;
}
/**
* Map an error code into an Error word, and log location.
*
* @param int $code Error code to map
* @return array Array of error word, and log location.
*/
function mapErrorCode($code) {
$error = $log = null;
switch ($code) {
case E_PARSE:
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$error = 'Fatal Error';
$log = LOG_ERR;
break;
case E_WARNING:
case E_USER_WARNING:
case E_COMPILE_WARNING:
case E_RECOVERABLE_ERROR:
$error = 'Warning';
$log = LOG_WARNING;
break;
case E_NOTICE:
case E_USER_NOTICE:
$error = 'Notice';
$log = LOG_NOTICE;
break;
case E_STRICT:
$error = 'Strict';
$log = LOG_NOTICE;
break;
case E_DEPRECATED:
case E_USER_DEPRECATED:
$error = 'Deprecated';
$log = LOG_NOTICE;
break;
default :
break;
}
return array($error, $log);
}
//calling custom error handler
set_error_handler("handleError");
print_r($arra); //undefined variable
print_r($dssdfdfgg); //undefined variable
include_once 'file.php'; //No such file or directory
?>
Keep in mind that, when attempting to set a statically-defined error handler on a namespaced class in PHP >= 5.3, you need to use the class namespace:
<?php
set_error_handler('\\My\\Namespace\\Bob::errorHandler');
?>
This may be of help to someone, who is/was looking for a way to get a backtrace of fatal errors such as maximum memory allocation issues, which can not be handled with user-defined functions, to pin-point the problem:
On a server hosting many sites that share common PHP includes, I set in one spot:
<?php
@ini_set ("error_log", "/my/path/php.err-" . $_SERVER ["HTTP_HOST"] . "-" . $_SERVER ["REMOTE_ADDR"] . "-" . $_SERVER ["REQUEST_METHOD"] . "-" . str_replace ("/", "|", $_SERVER ["REQUEST_URI"]));
?>
I actually used some additional information too from my software that I omitted, but that way, you'll find errors sorted more neatly in for example:-
/my/path/php.err-website.com-127.0.0.1-GET-path|index.html?xyz
And that at least helped me tremendously to then further pin-point where the problem is, as opposed to before just seeing the out of memory and not knowing which site/page it was on (as the PHP error only contains the very latest PHP code where it ran out of memory, which usually is just a shared included file, not the actual page).
i made an error handler that print also the backtrace and that can die on some errors. It can be useful if you want to die on every error you find.
<?php
function my_error_handler($errno, $errstr, $errfile, $errline){
$errno = $errno & error_reporting();
if($errno == 0) return;
if(!defined('E_STRICT')) define('E_STRICT', 2048);
if(!defined('E_RECOVERABLE_ERROR')) define('E_RECOVERABLE_ERROR', 4096);
print "<pre>\n<b>";
switch($errno){
case E_ERROR: print "Error"; break;
case E_WARNING: print "Warning"; break;
case E_PARSE: print "Parse Error"; break;
case E_NOTICE: print "Notice"; break;
case E_CORE_ERROR: print "Core Error"; break;
case E_CORE_WARNING: print "Core Warning"; break;
case E_COMPILE_ERROR: print "Compile Error"; break;
case E_COMPILE_WARNING: print "Compile Warning"; break;
case E_USER_ERROR: print "User Error"; break;
case E_USER_WARNING: print "User Warning"; break;
case E_USER_NOTICE: print "User Notice"; break;
case E_STRICT: print "Strict Notice"; break;
case E_RECOVERABLE_ERROR: print "Recoverable Error"; break;
default: print "Unknown error ($errno)"; break;
}
print ":</b> <i>$errstr</i> in <b>$errfile</b> on line <b>$errline</b>\n";
if(function_exists('debug_backtrace')){
//print "backtrace:\n";
$backtrace = debug_backtrace();
array_shift($backtrace);
foreach($backtrace as $i=>$l){
print "[$i] in function <b>{$l['class']}{$l['type']}{$l['function']}</b>";
if($l['file']) print " in <b>{$l['file']}</b>";
if($l['line']) print " on line <b>{$l['line']}</b>";
print "\n";
}
}
print "\n</pre>";
if(isset($GLOBALS['error_fatal'])){
if($GLOBALS['error_fatal'] & $errno) die('fatal');
}
}
function error_fatal($mask = NULL){
if(!is_null($mask)){
$GLOBALS['error_fatal'] = $mask;
}elseif(!isset($GLOBALS['die_on'])){
$GLOBALS['error_fatal'] = 0;
}
return $GLOBALS['error_fatal'];
}
?>
Usage :
<?php
error_reporting(E_ALL); // will report all errors
set_error_handler('my_error_handler');
error_fatal(E_ALL^E_NOTICE); // will die on any error except E_NOTICE
?>
We needed to use an error handler to handle SQL errors while passing the query along so the query is also logged and this is what we came up with, its kind of an ugly bridge but it works 100%
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline){
switch ($errno) {
case E_USER_ERROR:
if ($errstr == "(SQL)"){
// handling an sql error
echo "<b>SQL Error</b> [$errno] " . SQLMESSAGE . "<br />\n";
echo "Query : " . SQLQUERY . "<br />\n";
echo "On line " . SQLERRORLINE . " in file " . SQLERRORFILE . " ";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
} else {
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
}
exit(1);
break;
case E_USER_WARNING:
case E_USER_NOTICE:
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function sqlerrorhandler($ERROR, $QUERY, $PHPFILE, $LINE){
define("SQLQUERY", $QUERY);
define("SQLMESSAGE", $ERROR);
define("SQLERRORLINE", $LINE);
define("SQLERRORFILE", $PHPFILE);
trigger_error("(SQL)", E_USER_ERROR);
}
set_error_handler("myErrorHandler");
// trigger an sql error
$query = "SELECT * FROM tbl LIMIT 1";
$sql = @mysql_query($query)
or sqlerrorhandler("(".mysql_errno().") ".mysql_error(), $query, $_SERVER['PHP_SELF'], __LINE__);
?>
"The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called."
This is not exactly true. set_error_handler() can't handle them, but ob_start() can handle at least E_ERROR.
<?php
function error_handler($output)
{
$error = error_get_last();
$output = "";
foreach ($error as $info => $string)
$output .= "{$info}: {$string}\n";
return $output;
}
ob_start('error_handler');
will_this_undefined_function_raise_an_error();
?>
At work I have some code with errors that uncatched are the causes of integrity loss (people calling web services with file_get_contents that fails silently and afterwards insert garbage in the database).
here is the solution I found to transform a specific set of errors into exception and afterwards be able to selectively act (with the error code) regarding categories :
<?php
ini_set('error_reporting',E_ALL^E_NOTICE);
## first 10 bits reserved for the initial error number
define('EMASK',(~0)<<10);
define('ECODEMASK',~EMASK);
## categories
define('IOERROR', 1<<10);
define('EMPTYPARMS', 1<<11);
define('FAILURE', 1<<12);
## string error patterns => code
$catch_me=array(
"/^(file_get_contents)\((.*)\).*failed to open stream: (.*)/ " =>
array ( 'mesg' => "IO::Failed to open stream with",
'code' => IOERROR | FAILURE
),
"/^fopen\(.*\): Filename cannot be empty/" =>
array( 'msg' => "Parameters::empty",
'code' => EMPTYPARMS
)
);
function error_2_exception($errno, $errstr, $errfile, $errline,$context) {
global $catch_me;
foreach ($catch_me as $regexp => $res) {
if(preg_match($regexp,$errstr,$match)){
throw new Exception($res['mesg'],$res['code']|( $errno & EMASK ) );
}
}
/* switch back to PHP internal error handler */
return false;
}
## => want to catch this one
$f=file_get_contents("mlsdkfm");
## dont want to break existing wrong behaviour yet (so not caught)
$f=file_get_contents('');
## magic
set_error_handler("error_2_exception");
## behaviour remains the same
$f=file_get_contents('');
try {
## web services that dont work now raise an exception \o/
$f=file_get_contents("mlsdkfm");
} catch(Exception $e) {
## and I can group my exception by category
echo ( $e->getCode() & FAILURE ) ? "\nEPIC FAIL\n" : "\nbegnine";
}
?>
It is not possible to handle fatal errors with own handler. Even if you have set your own handler, fatal error will be always handled by PHP's default handler. The reason is the script may be in unstable state after fatal error occurence (details here: http://marc.theaimsgroup.com/?l=php-dev&m=97673386418430&w=2).
Two notes on using set_error_handler() on behaviour that I noticed when migrating an application from php 4.2.1 to php 4.3.9 (I do not yet have php 5.0 available, this might not apply there!).
1. setting the system error handler
If you want to set the standard php error handler again, after having set your own error handler, this works in php 4.2.1 by passing in an empty string:
<?php
function my_handler($log_level, $log_text, $error_file, $error_line)
{
// if an error occurs here, the standard error
// would be called (to avoid recursion)
// do something useful
// ...
}
$last_handler = set_error_handler("my_handler");
// after this, $last_handler == ""
// restore standard error handler
$last_handler = set_error_handler("");
// after this, $last_handler == "my_handler"
?>
The very same code now raises an error in php 4.3.9:
set_error_handler() expects argument 1, '', to be a valid callback
(Since the return value of the first call to set_error_handler() is still the empty string "", I don't see how this can be done any more. I don't really need this, because I use my own handlers as shown below, but it might be good to be aware of this.)
2. setting your own 'second level' handler
If you have set your own error handler, and want to replace it by another one (other than the standard php error handler) while it is being executed, note that the return value of set_error_handler when used INSIDE the error handler is "" instead of the name of the previous handler! This is not too surprising, because during execution of your self defined error handler, php replaces it with the standard php error handler to avoid infinite loops in case of problems inside the handler. This is only interesting if you want nested handlers as I do. Background of my design:
1st level handler: log into DB
2nd level handler: log into flat file (if log into DB fails)
3rd level handler: print to stdout (if log into flat file fails) (this is the sytem handler, finally).
<?php
function my_fallback_handler($log_level, $log_text, $error_file, $error_line)
{
// if an error occurs here, the standard error
// would be called (to avoid recursion)
// do something useful
// ...
} // my_fallback_handler
function my_handler($log_level, $log_text, $error_file, $error_line)
{
// if an error occurs here, the standard error
// would be called (to avoid recursion)
// but we want to have a fallback handler different
// to the standard error handler
$last_handler = set_error_handler("my_fallback_handler");
// I expected $last_handler == "my_handler"
// (which it would outside my_handler())
// but here it is the empty string ""
// do something useful
// ...
// now set the 1st level handler again:
// (do NOT use $last_handler as argument,
// because it equals "")
$last_handler = set_error_handler("my_handler");
} // my_handler
$last_handler = set_error_handler("my_handler");
?>
It seems that when you're letting PHP know that you have a custom error handler, you're not able to -update/set new- variables inside the class. Example:
<?php
class error {
var $error;
function error() {
$this->setIni(); // this causes PHP to ignore all other changes to the class.
}
function handler() {
echo $this->error.'!!';
}
function setText($text) {
$this->error = $text;
}
function setIni() {
set_error_handler(array($this, 'handler'));
}
}
$eh = new error;
$eh->setText('Error! <br>'); // this will not be saved
trigger_error('text', E_USER_ERROR);
// prints '!!'
?>
How it should be done:
<?php
class error {
var $error;
function error() {
// dont let PHP know of our error handler yet
}
function handler() {
echo $this->error.'!!';
}
function setText($text) {
$this->error = $text;
}
function setIni() {
set_error_handler(array($this, 'handler'));
}
}
$eh = new error;
$eh->setText('Error! <br>'); // this WILL work
$eh->setIni(); // call this method when you're ready with configuring the class. All other methods that will be called will have no effect on the errorHandling by PHP
trigger_error('text', E_USER_ERROR);
// prints 'Error! <br>!!'
?>
If you want to be sure that the native PHP error handler is called without resetting the handler stack (as set_error_handler(null) does), you can simply call set_error_handler with $error_types set to zero. This can be especially use full in conjunction with e.g. error_get_last():
<?php
// var_dump or anything else, as this will never be called because of the 0
set_error_handler('var_dump', 0);
@$undef_var;
restore_error_handler();
// error_get_last() is now in a well known state:
// Undefined variable: undef_var
... // Do something
$e = error_get_last();
...
?>
How to handle fatal errors in php 5.2:
<?php
register_shutdown_function('shutdownFunction');
function shutDownFunction() {
$error = error_get_last();
if ($error['type'] == 1) {
//do your stuff
}
}
?>
This might be handy if you don't want your clients to see the errors, and you do want to be one step ahead of them.
It emails you the errors even if it's a parse error.
set_error_handler() doesn't work for what I wanted.
<?php
ini_set('log_errors',TRUE);
ini_set('error_log','tiny_uploads/errors.txt');
if($_SERVER['REMOTE_ADDR'] != "YOUR IP ADDRESS"){
ini_set('display_errors',false);
}
function byebye(){
$dir = dirname(__FILE__);
if(file_exists($dir."/tiny_uploads/errors.txt")){
$errors = file_get_contents($dir."/tiny_uploads/errors.txt");
if(trim($errors)){
$head = "From: php_errors@".str_replace('www.','',$_SERVER['HTTP_HOST'])."\r\n";
$errors .= "---------------------------------------------\n\n";
$errors .= "\n\nServer Info:\n\n".print_r($_SERVER, 1)."\n\n";
$errors .= "---------------------------------------------\n\n";
$errors .= "\n\nCOOKIE:\n\n".print_r($_COOKIE, 1)."\n\n";
$errors .= "---------------------------------------------\n\n";
$errors .= "\n\nPOST:\n\n".print_r($_POST, 1)."\n\n";
$errors .= "---------------------------------------------\n\n";
$errors .= "\n\nGET:\n\n".print_r($_GET, 1)."\n\n";
mail("YOUR@EMAIL.COM","PHP Error ".$_SERVER['HTTP_HOST']."", $errors , $head);
$fp = fopen($dir."/tiny_uploads/errors.txt","w+");
fputs($fp, "");
fclose($fp);
}
}
}
register_shutdown_function("byebye");
?>
This actually works to catch Fatal errors...
<?php
function shutdown()
{
$a=error_get_last();
if($a==null)
echo "No errors";
else
print_r($a);
}
register_shutdown_function('shutdown');
ini_set('max_execution_time',1 );
sleep(3);
?>
it will output
Array ( [type] => 1 [message] => Maximum execution time of 1 second exceeded [file] => /path/to/file_name.php [line] => 136 )
Another way to catch PHP's fatal errors:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);
function shutdown(){
$isError = false;
if ($error = error_get_last()){
switch($error['type']){
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$isError = true;
break;
}
}
if ($isError){
echo "Script execution halted ({$error['message']})";
} else {
echo "Script completed";
}
}
register_shutdown_function('shutdown');
?>
Note that this will only catch runtime errors. So calling a method in a non existing class, or declaring a function twice does not trigger the shutdown handler.
error handling function that handles both errors and exceptions; also features a backtrace including possible function arguments.
<?php
$cfg = array();
$cfg['debug'] = 1;
$cfg['adminEmail'] = 'name@domain.tld';
function errorHandler($errno, $errstr='', $errfile='', $errline='')
{
// if error has been supressed with an @
if (error_reporting() == 0) {
return;
}
global $cfg;
// check if function has been called by an exception
if(func_num_args() == 5) {
// called by trigger_error()
$exception = null;
list($errno, $errstr, $errfile, $errline) = func_get_args();
$backtrace = array_reverse(debug_backtrace());
}else {
// caught exception
$exc = func_get_arg(0);
$errno = $exc->getCode();
$errstr = $exc->getMessage();
$errfile = $exc->getFile();
$errline = $exc->getLine();
$backtrace = $exc->getTrace();
}
$errorType = array (
E_ERROR => 'ERROR',
E_WARNING => 'WARNING',
E_PARSE => 'PARSING ERROR',
E_NOTICE => 'NOTICE',
E_CORE_ERROR => 'CORE ERROR',
E_CORE_WARNING => 'CORE WARNING',
E_COMPILE_ERROR => 'COMPILE ERROR',
E_COMPILE_WARNING => 'COMPILE WARNING',
E_USER_ERROR => 'USER ERROR',
E_USER_WARNING => 'USER WARNING',
E_USER_NOTICE => 'USER NOTICE',
E_STRICT => 'STRICT NOTICE',
E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR'
);
// create error message
if (array_key_exists($errno, $errorType)) {
$err = $errorType[$errno];
} else {
$err = 'CAUGHT EXCEPTION';
}
$errMsg = "$err: $errstr in $errfile on line $errline";
// start backtrace
foreach ($backtrace as $v) {
if (isset($v['class'])) {
$trace = 'in class '.$v['class'].'::'.$v['function'].'(';
if (isset($v['args'])) {
$separator = '';
foreach($v['args'] as $arg ) {
$trace .= "$separator".getArgument($arg);
$separator = ', ';
}
}
$trace .= ')';
}
elseif (isset($v['function']) && empty($trace)) {
$trace = 'in function '.$v['function'].'(';
if (!empty($v['args'])) {
$separator = '';
foreach($v['args'] as $arg ) {
$trace .= "$separator".getArgument($arg);
$separator = ', ';
}
}
$trace .= ')';
}
}
// display error msg, if debug is enabled
if($cfg['debug'] == 1) {
echo '<h2>Debug Msg</h2>'.nl2br($errMsg).'<br />
Trace: '.nl2br($trace).'<br />';
}
// what to do
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
return;
break;
default:
if($cfg['debug'] == 0){
// send email to admin
if(!empty($cfg['adminEmail'])) {
@mail($cfg['adminEmail'],'critical error on '.$_SERVER['HTTP_HOST'], $errorText,
'From: Error Handler');
}
// end and display error msg
exit(displayClientMessage());
}
else
exit('<p>aborting.</p>');
break;
}
} // end of errorHandler()
function displayClientMessage()
{
echo 'some html page with error message';
}
function getArgument($arg)
{
switch (strtolower(gettype($arg))) {
case 'string':
return( '"'.str_replace( array("\n"), array(''), $arg ).'"' );
case 'boolean':
return (bool)$arg;
case 'object':
return 'object('.get_class($arg).')';
case 'array':
$ret = 'array(';
$separtor = '';
foreach ($arg as $k => $v) {
$ret .= $separtor.getArgument($k).' => '.getArgument($v);
$separtor = ', ';
}
$ret .= ')';
return $ret;
case 'resource':
return 'resource('.get_resource_type($arg).')';
default:
return var_export($arg, true);
}
}
?>
Be careful with error-handlers if you are using require() with a function as argument, like
<?php
require(foo($foo));
?>
If the result of the function isn't a valid file to be included, you wont get any errormessage, but your script will crash without any output (see the bug-database for further details: http://bugs.php.net/bug.php?id=41862).
To ereg error in a text file
<?php
$log_file="log.txt";
set_error_handler(log_handler);
function log_handler ( $errno, $errstr, $errfile, $errline, $errcontext )
{
$context = var_export($errcontext, TRUE);
log_error_ereg("errno:$errno ($errstr) file:$errfile, line:$errline, context:$context\n");
}
function log_error_ereg($mess)
{
global $log_file;
$fd = fopen($log_file, 'a');
if(!$fd)
{
echo "<pre>$mess</pre>";
}
else
{
if(!fwrite($fd, date('Y-m-d H:i:s')." ERR : \n$mess\n\n"))
{
echo "<pre>$mess</pre>";
}
fclose($fd);
}
}
?>