dongshi2458 2016-05-12 12:01
浏览 55

正确的方法是不显示PHP错误,但将它们存储在数据库中

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.

  • 写回答

1条回答 默认 最新

  • douhuo1738 2016-05-12 17:10
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据