Skip to content

Instantly share code, notes, and snippets.

Created May 19, 2015 22:36
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/556c6c75daf83039b6f8 to your computer and use it in GitHub Desktop.
<?php
// This is my customized error handler : (excuse my english)
// Set these to 0 to prevent displaying errors to the users
// Set to 32767 to display all errors to the users
error_reporting(0);
ini_set('display_errors', 0);
function best_function_evaa($errno, $errstr, $errfile, $errline){
if (!(error_reporting() & $errno)){
// Code written here will run only if error_reporting is disabled
}
else{
// Code written here will run only if error_reporting is enabled
// Here you can have fun styling the errors by putting each type of error in a div element
// and styling it by using a <style> element at the end or by adding the style attribute directly to the div element
switch ($errno){
// Fatal errors :
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
echo '<b>ERROR</br> [' . $errno . '] : ' . $errstr . '<br/>';
echo 'Fatal error on line ' . $errline . 'in file ' . $errfile . '<br/>';
echo 'PHP ' . PHP_VERSION . ' (' . PHP_OS . ')<br/>';
echo 'Aborting...<br/>';
exit(1);
break;
// Compile-time parse errors :
case E_PARSE:
echo '<b>PARSE ERROR</b> [' . $errno . '] : ' . $errstr . '<br/>';
if (isset($errfile)){
echo 'File : ' . $errfile . '<br/>';
}
if (isset($errline)){
echo 'Line : ' . $errline . '<br/>';
}
break;
// Run-time warnings (non-fatal errors) :
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
echo '<b>WARNING</b> [' . $errno . '] : ' . $errstr . '<br/>';
if (isset($errfile)){
echo 'File : ' . $errfile . '<br/>';
}
if (isset($errline)){
echo 'Line : ' . $errline . '<br/>';
}
break;
// Run-time notices :
case E_NOTICE:
case E_USER_NOTICE:
echo '<b>NOTICE</b> [' . $errno . '] : ' . $errstr . '<br/>';
if (isset($errfile)){
echo 'File : ' . $errfile . '<br/>';
}
if (isset($errline)){
echo 'Line : ' . $errline . '<br/>';
}
break;
// Deprecated code that will not work in future versions :
case E_DEPRECATED:
case E_USER_DEPRECATED:
echo '<b>DEPRECATED</b> [' . $errno . '] : ' . $errstr . '<br/>';
if (isset($errfile)){
echo 'File : ' . $errfile . '<br/>';
}
if (isset($errline)){
echo 'Line : ' . $errline . '<br/>';
}
break;
// Unknown error type :
default:
echo 'Unknown error type: [' . $errno . '] : ' . $errstr . '<br/>';
if (isset($errfile)){
echo 'File : ' . $errfile . '<br/>';
}
if (isset($errline)){
echo 'Line : ' . $errline . '<br/>';
}
break;
}
}
// Now we will be storing the error informations into a file
// This variable will contain all the informations we need about the error
$error_log_info = '';
switch ($errno){
// \r\n must be bounded using " and not ' to return to line
// Here you can remove all of the "\r\n" and use HTML elements instead and style them if you
// intend to open this file with your browser.
// Fatal errors :
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$error_log_info = 'ERROR [' . $errno . '] : ' . $errstr . "\r\n";
$error_log_info .= 'Fatal error on line ' . $errline . 'in file ' . $errfile . "\r\n";
$error_log_info .= 'PHP ' . PHP_VERSION . ' (' . PHP_OS . ')' . "\r\n";
$error_log_info .= 'Aborting...' . "\r\n";
exit(1);
break;
// Compile-time parse errors :
case E_PARSE:
$error_log_info = 'PARSE ERROR [' . $errno . '] : ' . $errstr . "\r\n";
$error_log_info .= 'File : ' . $errfile . "\r\n";
$error_log_info .= 'Line : ' . $errline . "\r\n";
break;
// Run-time warnings (non-fatal errors) :
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
$error_log_info = 'WARNING [' . $errno . '] : ' . $errstr . "\r\n";
$error_log_info .= 'File : ' . $errfile . "\r\n";
$error_log_info .= 'Line : ' . $errline . "\r\n";
break;
// Run-time notices :
case E_NOTICE:
case E_USER_NOTICE:
$error_log_info = 'NOTICE [' . $errno . '] : ' . $errstr . "\r\n";
$error_log_info .= 'File : ' . $errfile . "\r\n";
$error_log_info .= 'Line : ' . $errline . "\r\n";
break;
// Deprecated code that will not work in future versions :
case E_DEPRECATED:
case E_USER_DEPRECATED:
$error_log_info = 'DEPRECATED [' . $errno . '] : ' . $errstr . "\r\n";
$error_log_info .= 'File : ' . $errfile . "\r\n";
$error_log_info .= 'Line : ' . $errline . "\r\n";
break;
// Unknown error type :
default:
$error_log_info = 'Unknown error type: [' . $errno . '] : ' . $errstr . "\r\n";
$error_log_info .= 'File : ' . $errfile . "\r\n";
$error_log_info .= 'Line : ' . $errline . "\r\n";
break;
}
// Save the date and time of the error
$error_log_info .= 'Date : ' . date('d-m-Y h:i:s a', time()) . "\r\n";
$error_log_info .= 'Time Zone : ' . date_default_timezone_get() . "\r\n\r\n\r\n";
// I want the file name to be the date of occurrence of the error. You can set this to whatever you want
$error_log_file_name = date('Y-m-d', time());
// We open the file to read from it (using c+b) //
$error_log_file = fopen('path/to/the/file/' . $error_log_file_name . '.txt/log...', 'c+b') or die('Unable to open file!');
// Change the path and the file extension above to suit your needs
while (!feof($error_log_file)){
// If the file exists and contains some data then we will copy it to our variable
$error_log_info .= fgets($error_log_file);
// Actually we can write the error informations directly (by opening the file using ab) but this
// will cause the informations that belongs to the last error to be written at the end of the file and
// you will have to scroll to the end to reach it, so I think that it would be more comfortable if you
// open the file and find the last errors that occurred in the top of the file.
}
fclose($error_log_file);
// We open the file again to clear it and write the new data (using wb)
$error_log_file = fopen('path/to/the/file/' . $error_log_file_name . '.txt/log...', 'wb') or die('Unable to open file!');
// Again, change the path and the file extension above to suit your needs
fwrite($error_log_file, $error_log_info);
fclose($error_log_file);
// Return true to avoid running the PHP internal error handler
return true;
}
// Now we set our new error handler
$old_error_handler = set_error_handler('best_function_evaa');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment