46

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.

3
  • 1
    Try to explicitly set it via ini_set() function in your script. However it's a bad practice and you should catch the errors and do something with them instead of hiding via display_Errors
    – Royal Bg
    Commented Apr 11, 2013 at 12:54
  • 2
    I suggest running grep display_errors /path/to/php.ini to see if your line is getting overrided somewhere down the file. This is what was happening to me.
    – rgajrawala
    Commented Aug 21, 2015 at 18:02
  • @rgajrawala your comment was the life saver for me, thank you Commented Aug 2, 2021 at 2:56

12 Answers 12

36

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 development
    log_errors(false);
}
else
{
    // You don't want to display errors on a production environment
    display_errors(false);

    // You definitely want to 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 (development, test, acceptance, and production) and change your settings accordingly.

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

4
  • 1
    error_reporting(0); does have an effect on logging be carefull !
    – Korri
    Commented Nov 18, 2014 at 16:08
  • @Korri is right. Refer to this answer for detailed information: stackoverflow.com/questions/1911920/…
    – Sherlock
    Commented Nov 18, 2014 at 20:32
  • 3
    Am I missing something or is display_errors some kind of custom function? I don't see it in the manual.
    – Dominic P
    Commented Nov 18, 2014 at 21:58
  • 1
    It's a runtime configuration function: php.net/manual/en/…
    – Sherlock
    Commented Nov 18, 2014 at 22:49
31

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');
2
  • 5
    Bit of an aside but I think it's preferable to set boolean values to '1'/'0', rather than 'On'/'Off'. For the simple reason that when you read the value back with init_get() you get the literal string that was set. 'Off' is a non-empty string that evaluates to True.
    – MrWhite
    Commented May 6, 2015 at 16:40
  • 2
    It would probably make it easier, since one would be able to just write if(ini_get('display_errors')) (which right now would be true either way). But I actually think the check should never be done like this either way! Anyone maintaining the code could still set it to Off or No (or even Foobar (anything but 1, Yes and On works as negative value)). A safe check could be for example: $val = strtolower(ini_get('display_errors')); if ($val == '1' || $val == 'on' || $val == 'yes') ... Still you have a very valid point, imho!
    – Levite
    Commented May 6, 2015 at 20:08
13

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 (i.e., like ;error_reporting = E_ALL & ~E_NOTICE)

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

1
  • Will this affect error logging? as i want errors to be logged but not displayed
    – Jms Bnd
    Commented Apr 11, 2013 at 13:11
10

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

error_reporting = off
1
  • 1
    How to find php.ini, in console write php -ini | less and find the path for php.ini, or create a file.php and put inside <?php phpinfo(); ?> Commented Apr 3, 2018 at 18:16
5

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.

3
  • Nice summary, and also nice set_error_handler hack ^^
    – Levite
    Commented Jan 14, 2015 at 12:32
  • It may also be e.g. /etc/opt/remi/php73/php.ini (for PHP 7.3) on some hosting (Red Hat Linux based?) Commented Apr 22, 2020 at 3:30
  • to find what .ini files your php loads, try php -i | grep ini on a command line (this shows ini files loaded for php-cli, so you may also need to check php-fpm and/or apache/webserver too, as directives can be in many places)
    – Sandra
    Commented Jul 1 at 11:40
4

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
1
  • 1
    Where is this file located? Commented Apr 22, 2020 at 3:32
3

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
1
  • Is this for php.ini or htaccess?
    – Jms Bnd
    Commented Apr 11, 2013 at 12:56
2

Turn if off:

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

1

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

0

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.

0

It is not enough in case of PHP fpm. It was one more configuration file which can enable display_error. You should find www.conf. In my case it is in directory /etc/php/7.1/fpm/pool.d/

You should find php_flag[display_errors] = on and disable it, php_flag[display_errors] = off. This should solve the issue.

0

It's been quite some time and iam sure OP's answer is cleared. If any new user still looking for answer and scrolled this far, then here it is.

Check your updated information in php.ini file

Using Windows explorer:

C/xampp/php/php.ini

Using XAMPP Control Panel

  1. Click the Config button for 'Apache' (Stop | Admin | Config | Logs)
  2. Select PHP (php.ini)

Can i create my own phpinfo()? Yes you can

  1. Create a phpinfo.php in your root directly or anywhere you want
  2. Place this <?php phpinfo(); ?>
  3. Save the file.
  4. Open the file and you should see all the details.

How to set display_errors to Off in my own file without using php.ini

You can do this using ini_set() function. Read more about ini_set() here (https://www.php.net/manual/en/function.ini-set.php)

  1. Go to your header.php or index.php
  2. add this code ini_set('display_errors', FALSE);

To check the output without accessing php.ini file

$displayErrors = (ini_get('display_errors') == 1) ? 'On' : 'Off';
$PHPCore = array(
    'Display error' => $displayErrors
    // add other details for more output
);
foreach ($PHPCore as $key => $value) {
    echo "$key: $value";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.