I have the code below in an include file (slightly modified this one to remove custom code).
You can use it to log error messages to a database. It uses a class to handle DB interaction, but you can change that to your own implementation. Or log it to a file.
It also outputs information to the browser, but you can discard this aswell in a production environment.
<?php
set_error_handler("ErrorHandler");
register_shutdown_function('ShutdownHandler');
function ShutdownHandler() {
$error = error_get_last();
if($error !== NULL){
switch ($error['type']) {
case E_WARNING:exit;
}
$context = get_defined_vars();
ErrorHandler($error['type'],'(SHUTDOWN) ' . $error['message'], $error['file'],$error['line'],$context);
}
}
function ErrorHandler($error_level,$error_message, $error_file,$error_line,$error_context)
{
$msg = "";
$type = FriendlyErrorType($error_level);
switch ($error_level) {
case E_USER_ERROR:
$msg .= "<div style='border:1px solid #F00;padding:10px;margin:10px;'>";
$msg .= "<h1>Error</h1>";
$msg .= "<p><b>[".$error_level."]</b>: ". $error_message . "</p>";
$msg .= "<p><b>File</b>: ". $error_file . "<br/>";
$msg .= "<b>Line</b>: ". $error_line . "</p></div>";
case E_USER_WARNING:
$msg .= "<p>Warning -> <b>[".$error_level."]</b>".$error_file." on line ". $error_line .": ". $error_message . "</p>";
break;
case E_USER_NOTICE:
$msg .= "<p>Notice -> <b>[".$error_level."]</b>".$error_file." on line ". $error_line .": ". $error_message . "</p>";
break;
default:
$msg .= "<p>".$type." -> <b>[".$error_level."]</b>".$error_file." on line ". $error_line .": ". $error_message . "</p>";
break;
}
$context = print_r($error_context, true);
$sVars = print_r($_SERVER, true);
$pVars = print_r($_POST, true);
$qVars = print_r($_GET, true);
//add to database
//custom class to add info to a database. You need to alter this to your own INSERT method.
//ct2database::query("INSERT INTO ct2_errors (`DateCreated`,`Type`,`Level`,`Message`,`Line`,`File`,`Context`,`ServerVars`,`PostVars`,`QueryVars`) VALUES ('$1','$2','$3','$4','$5','$6','$7','$8','$9','$10')", MysqlDate(), $type, $error_level,$error_message,$error_line,$error_file,$context, $sVars, $pVars, $qVars);
if ($error_level == E_USER_ERROR)
exit;
}
function FriendlyErrorType($type)
{
switch($type)
{
case E_ERROR: // 1 //
return 'E_ERROR';
case E_WARNING: // 2 //
return 'E_WARNING';
case E_PARSE: // 4 //
return 'E_PARSE';
case E_NOTICE: // 8 //
return 'E_NOTICE';
case E_CORE_ERROR: // 16 //
return 'E_CORE_ERROR';
case E_CORE_WARNING: // 32 //
return 'E_CORE_WARNING';
case E_CORE_ERROR: // 64 //
return 'E_COMPILE_ERROR';
case E_CORE_WARNING: // 128 //
return 'E_COMPILE_WARNING';
case E_USER_ERROR: // 256 //
return 'E_USER_ERROR';
case E_USER_WARNING: // 512 //
return 'E_USER_WARNING';
case E_USER_NOTICE: // 1024 //
return 'E_USER_NOTICE';
case E_STRICT: // 2048 //
return 'E_STRICT';
case E_RECOVERABLE_ERROR: // 4096 //
return 'E_RECOVERABLE_ERROR';
case E_DEPRECATED: // 8192 //
return 'E_DEPRECATED';
case E_USER_DEPRECATED: // 16384 //
return 'E_USER_DEPRECATED';
}
return "";
}
?>