Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off display errors using file "php.ini"

Tags:

php

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.

like image 689
Jms Bnd Avatar asked Apr 11 '13 12:04

Jms Bnd


People also ask

How do I hide warnings and notices in PHP?

In the current file, search for the line of code error_reporting. There will be a line of Default Value: E_ALL as shown below: Replace this line of code with Default Value: E_ALL & ~E_NOTICE. It will display all the errors except for the notices.

How does PHP handle notice error?

By default, PHP sends an error log to the server's logging system or a file, depending on how the error_log configuration is set in the php. ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.

How do I enable PHP error logging?

Enable Error Logging in php. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Now, you must enable only one statement to parse the log errors in the php.


2 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 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.

like image 158
Sherlock Avatar answered Oct 04 '22 14:10

Sherlock


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'); 
like image 45
Levite Avatar answered Oct 04 '22 14:10

Levite