Skip to content

Instantly share code, notes, and snippets.

@fabiocicerchia
Created June 25, 2013 13:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiocicerchia/5858359 to your computer and use it in GitHub Desktop.
Save fabiocicerchia/5858359 to your computer and use it in GitHub Desktop.
PHP - Highlight PHP errors
<?php
function checkErrors()
{
$errorMessage = 'This web page contains errors! Go and check it out in the error_log (' . ini_get('error_log') . ').';
$outputContent = ob_get_clean();
$errors = error_get_last();
// If there is at least one error...
if (!empty($errors)) {
// ... and is not an AJAX call ...
if (!$GLOBALS['isAjaxRequest']) {
// ... add a div to the web page.
$outputContent .= '<div style="background-color: #F00; position: absolute; padding: 10px; font-size: 13px; color: #FFF; font-weight: bold; width: 100%; text-align: center; top: 0px; text-decoration: blink;">' . $errorMessage . '</div>';
$outputContent .= '<style type="text/css">html { margin-top: 30px !important; }</style>';
} else {
// ... adapt to show an error message in case of AJAX call.
}
}
// Then print the entire response.
echo $outputContent;
}
// Execute the function at the end of the execution.
register_shutdown_function('checkErrors');
// Check if the HTTP request is an AJAX call.
$GLOBALS['isAjaxRequest'] = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
// Start the output buffering
ob_start();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment