I have a config to be used in production (not development) PHP site, where I want to hide all PHP errors to the user, but store them in a table in the database. I have the following config, but for some reason if I change the values of the defined constants (customized) to FALSE, I don't get the display error, but neither the database loging:
error_reporting(SHOW_E_ALL); // SHOW_E_ALL = 0 for production
ini_set('display_errors', SHOW_ERRORS); // SHOW_ERRORS = FALSE for production
ini_set('display_startup_errors', SHOW_ERRORS); // SHOW_ERRORS = FALSE for production
set_error_handler('myErrorFunction', E_ALL);
I have tried different combinations, but I get either the error display in the browser navigator and also the database log of the error, or no display and no log, and never the combination that I want: no display and error log into the database.
I have found the solution. The display errors properties must be activated, and the customized error-handler function must not display the error and handle properly, I had forgotten not to hide the error print:
define("DEBUG", FALSE); // set TRUE for debugging purposes
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
set_error_handler('myErrorFunction', E_ALL);
function myErrorFunction($errno, $errstr, $errfile, $errline){
if (!(error_reporting() & $errno)) {
return;
}
$conn = MySQLiConnection::getConnection();
// insert into database
....
....
if(DEBUG){ // if true, it displays the error
echo "<font color='red'>PHP error in line $errline of file $errfile: " . $errno . " " . str_replace('\\', '/', addslashes($errstr)). '</font>';
}else{
// nothing
}
return true;
} // function