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.
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.
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');
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.
In file php.ini you should try this for all errors:
error_reporting = off
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.
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
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
Turn if off:
You can use error_reporting(); or put an # in front of your fileopen().
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
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.
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.
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
Click the Config button for 'Apache' (Stop | Admin | Config | Logs)
Select PHP (php.ini)
Can i create my own phpinfo()? Yes you can
Create a phpinfo.php in your root directly or anywhere you want
Place this <?php phpinfo(); ?>
Save the file.
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)
Go to your header.php or index.php
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";
}
For some reason my Mac is displaying the error message "Undefined index: an-undefined-key" whereas on Windows with WAMP it gracefully ignores the exception.
Example of usage:
if ($some_array['an-undefined-key']) {
// ...
}
Is there a PHP configuration which changes this behaviour?
I realise that I can do the following, but I would rather use the above technique if possible:
if (isset($some_array['an-undefined-key']) && $some_array['an-undefined-key']) {
// ...
}
Yes, you need to adjust the error_reporting and display_errors settings which sounds like they are different between your two different environments.
Here are links
error_reporting
PHP ini_set - look at first example for display_errors
The difference between the PHP on your Mac using the installed repo and your WAMP server is the php.ini, try editing your script and append the following lines to the top:
ini_set('display_errors', '1');
error_reporting(E_ALL);
This should be on your scripts. If this is not the problem, then please comment back and I will change accordingly.
You should not add unnecessary line to your script as you will have to change them when you go live.
Edit you php.ini ( using the wampmanager menus so you get the correct file )
left click wampmanager -> PHP -> php.ini
Look for these parameters and change to these values
error_reporting = E_ALL
This one makes error show on the web page
display_errors = On
error_log = "c:/wamp/logs/php_error.log"
this one will make the errors log into the file specified by error_log even if they dont show on the screen
log_errors = On
WAMP comes with XDEBUG configured so you should now see big orange errors in the web page
Check the bottom of the php.ini file for this line. Numbers for versions may differ on your system
; XDEBUG Extension
zend_extension = "c:/wamp/bin/php/php5.X.Y/zend_ext/php_xdebug-2.2.0-5.3-vc9.dll"
I have a WAMP 2.2 server running on a Windows 7 box and cannot get PHP error logging working at all.
The file is always blank even after I explicitly trigger USER_ERROR errors, or cause normal ERROR errors.
I'm including the error relevant sections of the php.ini file - hopefully you can find something:
error_reporting = E_ALL
error_log = "c:/wamp32/logs/php_error.log" ;(UNCOMMENTED BY ME)
log_errors = On
display_errors = On
The line ; log_errors is just a comment for the following block, for the purpose of showing you what the settings are in dev vs production. You uncommented four lines which aren't meant to control anything, and I'm surprised your Apache service doesn't have problems starting up because of it.
What you need to do is look for the line:
log_errors = Off
And change the value to On
That said, once you restart the Apache service, the settings should take effect. However, I was unable to get WampServer to properly log php errors despite these settings. Apache will not start up when I specify the error_log parameter.
For me it turned out to be a permissions error. I ended up giving EVERYONE full control of the error log file and it seemed to fix my issue. Best of luck.
Did you try adding these lines to your php file?
ini_set("display_errors", "1");
ini_set("log_errors", "1");
ini_set("error_log", "/wamp64/logs/php_error.log");
I have php v5.3.2 on ubuntu 10.04. i changed the reporting options in /etc/php5/apache2/php.ini to these:
display_errors = 1
error_reporting = E_ALL
I also added these lines to the top of my php files and restarted apache but im still not able to see the errors/warnings,
ini_set('display_errors',1);
error_reporting(E_ALL);
Is there anything else in the way that prevents the errors from showing up?
Edit 1: According to phpinfo(), both display_errors and display_startup_errors are on.
The value of error_reporting is also 30719 which i'm not quite sure what it means.
Could you try error_reporting(-1); ?
I have installed xampp on ubuntu. PHP is configured to show all possible errors, warnings, notices etc, but when i make an error in php code no error is displayed.
When i copy that file to other computer (debian with native apache, mysql and php set) and open it in browser it shows
Fatal error: Call to a member function fetch() on a non-object in...
as expected, so why xampp with identical php.ini shows just an empty page?
Another solution is
add this line in your .htaccess file
php_value display_errors on
there may be some serever configure mistake
write below as first line at your php page
ini_set('error_reporting', E_ALL);
ini_set( 'display_errors', 1 );
NOTE: use Xdebug
Find your php.ini file. You can find the location by typing php --ini in the terminal.
Then look for display_errors and set it to 1. And error_reporting to E_ALL.
This will set the value globally.
If you only need to see errors for a particular script or application:
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/errorfunc.configuration.php