Home » Php » Turn Off Display Error PHP.ini

Turn Off Display Error PHP.ini

Posted by: admin November 29, 2017 Leave a comment

Questions:

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 I am missing? I have tried the following in my php.ini file:

;Error display
display_startup_errors = Off
display_errors = Off
html_errors = Off
docref_root = 0
docref_ext = 0

For some reason when I do a fileopen() call for a file which does not exist, I still get the error displayed. This is not safe for a live website, for obvious reasons.

Thanks

Answers:

I always use something like this in a configuration file:

// toggle this to change the setting
define('DEBUG', true); 
// you want all errors to be triggered
error_reporting(E_ALL); 

if(DEBUG == true)
{
    // you're developing, so you want all errors to be shown
    display_errors(true);
    // logging is usually overkill during dev
    log_errors(false); 
}
else
{
    // you don't want to display errors on a prod environment
    display_errors(false); 
    // you definitely wanna log any occurring
    log_errors(true); 
}

This allows easy toggling between debug settings. You can improve this further by checking on which server the code is running (dev, test, acceptance, prod) and change your settings accordingly.

Note that no errors will be logged if error_reporting is set to 0, as cleverly remarked by Korri.

Questions:
Answers:

You should consider not displaying your error messages instead!

Set ini_set('display_errors', 'Off'); in your php code (or directly into your ini 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');

Questions:
Answers:

In php.ini Comment out,

error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
error_reporting = E_ALL & ~E_NOTICE

By placing a ; ahead of it ( ie like ;error_reporting = E_ALL & ~E_NOTICE)

For disabling in a single file, place error_reporting(0); after opening a php tag.

Questions:
Answers:

in php.ini you should try this for all errors

error_Reporting = off

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

in php.ini you should try this for all errors

display_errors = On

Questions:
Answers:

Why to turn off?

You can use error_reporting();http://php.net/manual/en/function.error-reporting.php or put an @ – http://www.php.net/manual/en/language.operators.errorcontrol.php in front of your fileopen().

Questions:
Answers:

I usually use php’s built in error handlers that can handle every possible error outside of syntax and still render a nice ‘Down for maintenance’ page otherwise:

Format PHP error on production server

Questions:
Answers:

Open your php.ini file (If you are using Linux – sudo vim /etc/php5/apache2/php.ini)

Add this lines into that file

   error_reporting = E_ALL & ~E_WARNING 

(If you need to disabled any other errors -> error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING)

    display_errors = On

And finally you need to restart your APACHE server.

Leave a Reply

Your email address will not be published. Required fields are marked *