Turn off display errors using file “php.ini”

前端 未结 12 1266
春和景丽
春和景丽 2020-11-28 06:59

I am trying to turn off all errors on my website. I have followed different tutorials on how to do this, but I keep getting read and open error messages. Is there something

相关标签:
12条回答
  • 2020-11-28 07:24

    You can also use PHP's error_reporting();

    // Disable it all for current call
    error_reporting(0);
    

    If you want to ignore errors from one function only, you can prepend a @ symbol.

    @any_function(); // Errors are ignored
    
    0 讨论(0)
  • 2020-11-28 07:25

    Turn if off:

    You can use error_reporting(); or put an @ in front of your fileopen().

    0 讨论(0)
  • 2020-11-28 07:26

    Let me quickly summarize this for reference:

    • error_reporting() adapts the currently active setting for the default error handler.

    • Editing the error reporting ini options also changes the defaults.

      • Here it's imperative to edit the correct php.ini version - it's typically /etc/php5/fpm/php.ini on modern servers, /etc/php5/mod_php/php.ini alternatively; while the CLI version has a distinct one.

      • Alternatively you can use depending on SAPI:

        • mod_php: .htaccess with php_flag options
        • FastCGI: commonly a local php.ini
        • And with PHP above 5.3 also a .user.ini

      • Restarting the webserver as usual.

    If your code is unwieldy and somehow resets these options elsewhere at runtime, then an alternative and quick way is to define a custom error handler that just slurps all notices/warnings/errors up:

    set_error_handler(function(){});
    

    Again, this is not advisable, just an alternative.

    0 讨论(0)
  • 2020-11-28 07:33

    In file php.ini you should try this for all errors:

    error_reporting = off
    
    0 讨论(0)
  • 2020-11-28 07:36

    You should consider not displaying your error messages instead!

    Set ini_set('display_errors', 'Off'); in your PHP code (or directly into your ini file if possible), and leave error_reporting on E_ALL or whatever kind of messages you would like to find in your logs.

    This way you can handle errors later, while your users still don't see them.

    Full example:

    define('DEBUG', true);
    error_reporting(E_ALL);
    
    if (DEBUG)
    {
        ini_set('display_errors', 'On');
    }
    else
    {
        ini_set('display_errors', 'Off');
    }
    

    Or simply (same effect):

    define('DEBUG', true);
    
    error_reporting(E_ALL);
    ini_set('display_errors', DEBUG ? 'On' : 'Off');
    
    0 讨论(0)
  • 2020-11-28 07:42

    In file php.ini you should try this for all errors:

    display_errors = On
    

    Location file is:

    • Ubuntu 16.04:/etc/php/7.0/apache2
    • CentOS 7:/etc/php.ini
    0 讨论(0)
提交回复
热议问题