我有一个配置要在生产(而不是开发)PHP站点中使用,我想在那里向用户隐藏所有PHP错误,但将它们存储在数据库的表中。我有以下配置,但出于某种原因,如果我将定义的常量(自定义)的值更改为FALSE,我不会得到显示错误,但也不会得到数据库登录:
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);
我尝试过不同的组合,但我要么在浏览器导航器中显示错误,还得到错误的数据库日志,要么没有显示也没有日志,而且从来没有我想要的组合:没有显示,错误日志进入数据库。
我找到了解决方案。必须激活显示错误属性,并且自定义错误处理程序功能不能正确显示错误和处理,我忘记了不要隐藏错误打印:
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