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";
}
Related
I'm running ubuntu 10.04 + nginx + php-fpm 5.4
If I set display_errors = On in php.ini all errors are printed. If Instead I set that to off and then use ini_set('display_errors, '1'); directly in the script they will show as well but not the parse errors, just a blank page. I tried to play with error_reporting too and E_STRICT but I couldn't find a way!
If you disable display_errors in php.ini, and later enable it in your PHP script using ini_set(), it will only be enabled after the line containing that ini_set() call has been executed.
Parse errors occur before the PHP script even starts -- when the PHP file is parsed (hence the "parse error" name).
Which means they occur before your ini_set() has even a chance of being executed -- which means that, in your case, display_errors is not enabled when the parse error occurs ; and, as a consequence, you don't get anything displayed.
here I am years after this was answered, but I found a way around this.
For the script I'm writing, I created a second script which includes the ini_set() directives, followed by an include for the script I really am working on.
To with, here is test_run.php
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
include('./my_script.php');
?>
Aside from turning on display_errors, you can also watch error logs. Typically, running Ubuntu + apache, your error log is going to be in /var/log/apache2/error_log. To watch in real time what's happening, you run something like tail -f /var/log/apache2/error_log.
This can sometimes be more straightforward than fussing with php settings.
**You must enable error displaying in the php.ini file **
I have added ( un-commented ) the following lines, and the parse errors are being displayed now.
error_reporting
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Try error_reporting(E_ALL);. Or the docs
I'm running ubuntu 10.04 + nginx + php-fpm 5.4
If I set display_errors = On in php.ini all errors are printed. If Instead I set that to off and then use ini_set('display_errors, '1'); directly in the script they will show as well but not the parse errors, just a blank page. I tried to play with error_reporting too and E_STRICT but I couldn't find a way!
If you disable display_errors in php.ini, and later enable it in your PHP script using ini_set(), it will only be enabled after the line containing that ini_set() call has been executed.
Parse errors occur before the PHP script even starts -- when the PHP file is parsed (hence the "parse error" name).
Which means they occur before your ini_set() has even a chance of being executed -- which means that, in your case, display_errors is not enabled when the parse error occurs ; and, as a consequence, you don't get anything displayed.
here I am years after this was answered, but I found a way around this.
For the script I'm writing, I created a second script which includes the ini_set() directives, followed by an include for the script I really am working on.
To with, here is test_run.php
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
include('./my_script.php');
?>
Aside from turning on display_errors, you can also watch error logs. Typically, running Ubuntu + apache, your error log is going to be in /var/log/apache2/error_log. To watch in real time what's happening, you run something like tail -f /var/log/apache2/error_log.
This can sometimes be more straightforward than fussing with php settings.
**You must enable error displaying in the php.ini file **
I have added ( un-commented ) the following lines, and the parse errors are being displayed now.
error_reporting
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Try error_reporting(E_ALL);. Or the docs
So, I have 500 error on a page and I want more information why I do have error (because the page works fine in local but not on my server)
I try the following solution preconised in This topic but it doesn't help. I restarted nginx everytime and no changes.
Here is the folder on my server (VPS, no cPanel) :
here is my actual state of php.ini :
http://pastebin.fr/75379
Any clue ?
What you placed at the top of your PHP.INI file is what you would use in an actual .php file to turn on error reporting / show errors on a per file bases. You do not add that to your PHP.INI file.
Example of use in a .php file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
If you search your PHP.INI file you will find error_reporting = E_ALL, and display_errors = On are set.
Notice the definitions in the PHP.INI file:
; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments. Depending on the code
; which is triggering the error, sensitive information could potentially leak
; out of your application such as database usernames and passwords or worse.
; For production environments, we recommend logging errors rather than
; sending them to STDOUT.
; Possible Values:
; Off = Do not display any errors
; stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
; On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
display_errors = On
You should not display errors in a production environment. You don't want to reveal any extra information to any potential hackers. Also these errors will mean nothing to your users. You should just show a generic error page and log all your errors for your review.
In a development environment I would have them all on and display_startup_errors = On.
I see log_errors = On so you should be getting your error messages logged to:
/var/log/nginx/default-error.log. I suggest looking there to find your issue after you fix your PHP.INI file.
I'm trying to get the warnings to show in my php error log.
My ISP has some warnings showing and I need to be able to see them on my test server.
error_reporting = E_ALL & ~E_NOTICE
display_errors = off
display_startup_errors = Off
log_errors = On
error_log = "C:\php-errors.txt"
I've upgraded my php version too 5.2.17
I also have
register_globals = Off
Like my ISP, but I can't get any warnings to show.
ini_set('display_errors', 'on'); is a great way to change php configurations settings specific for that page only. Include it in a global header/initialization file to make it application specific. Also, as mentioned before error_reporting(E_ALL); is good for this too.
Code at the top your scripts:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Be sure to use these only for development environments only.
Set display_errors to On to display the warnings in the page (instead of in the logfile) and set error_reporting to E_ALL | E_STRICT to display all warnings and errors in your php.ini.
If you want to show all possible warnings, try error_reporting(-1);, or you can put error_reporting = -1 into the php.ini file on your test server. It works, because the internal variable is used as a bit-field, and -1 sets all the bits on, hence, showing all possible errors.
To make sure that the error_reporting is still set to what you think, the variable returns the currently set level.
$prevErrLevel = error_reporting(-1);
echo "errlevel was: $prevErrLevel before setting to all.", __FILE__,':',__LINE__;
I need to setup my PHP script at the top to disable error reporting for strict standards.
Can anybody help ?
Do you want to disable error reporting, or just prevent the user from seeing it? It’s usually a good idea to log errors, even on a production site.
# in your PHP code:
ini_set('display_errors', '0'); # don't show any errors...
error_reporting(E_ALL | E_STRICT); # ...but do log them
They will be logged to your standard system log, or use the error_log directive to specify exactly where you want errors to go.
For no errors.
error_reporting(0);
or for just not strict
error_reporting(E_ALL ^ E_STRICT);
and if you ever want to display all errors again, use
error_reporting(-1);
All above solutions are correct. But, when we are talking about a normal PHP application, they have to included in every page, that it requires. A way to solve this, is through .htaccess at root folder.
Just to hide the errors. [Put one of the followling lines in the file]
php_flag display_errors off
Or
php_value display_errors 0
Next, to set the error reporting
php_value error_reporting 30719
If you are wondering how the value 30719 came, E_ALL (32767), E_STRICT (2048) are actually constant that hold numeric value and (32767 - 2048 = 30719)
The default value of error_reporting flag is E_ALL & ~E_NOTICE if not set in php.ini.
But in some installation (particularly installations targeting development environments) has E_ALL | E_STRICT set as value of this flag (this is the recommended value during development). In some cases, specially when you'll want to run some open source projects, that was developed prior to PHP 5.3 era and not yet updated with best practices defined by PHP 5.3, in your development environment, you'll probably run into getting some messages like you are getting. The best way to cope up on this situation, is to set only E_ALL as the value of error_reporting flag, either in php.ini or in code (probably in a front-controller like index.php in web-root as follows:
if(defined('E_STRICT')){
error_reporting(E_ALL);
}
In php.ini set :
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
WordPress
If you work in the wordpress environment, Wordpress sets the error level in file wp-includes/load.php in function wp_debug_mode(). So you have to change the level AFTER this function has been called ( in a file not checked into git so that's development only ), or either modify directly the error_reporting() call
I didn't see an answer that's clean and suitable for production-ready software, so here it goes:
/*
* Get current error_reporting value,
* so that we don't lose preferences set in php.ini and .htaccess
* and accidently reenable message types disabled in those.
*
* If you want to disable e.g. E_STRICT on a global level,
* use php.ini (or .htaccess for folder-level)
*/
$old_error_reporting = error_reporting();
/*
* Disable E_STRICT on top of current error_reporting.
*
* Note: do NOT use ^ for disabling error message types,
* as ^ will re-ENABLE the message type if it happens to be disabled already!
*/
error_reporting($old_error_reporting & ~E_STRICT);
// code that should not emit E_STRICT messages goes here
/*
* Optional, depending on if/what code comes after.
* Restore old settings.
*/
error_reporting($old_error_reporting);