I've re-configured my Apache and now I want it to run in a better way.
To be more precise, I want to configure php.ini in such a manner that errors will only be displayed when I specify error_reporting so by default they wouldn't be displayed. This is because I'm developing a platform and I want to see errors as I go, then stop them for showing without having to go through the php.ini hassle.
I tried display_errors = Off but that wouldn't display errors even after I specify error_reporting level.
Ideas ?
First of all you should learn more about how you have to configurate your php.ini and know which command do what.
If you deactivate to display the errors there will be no errors displayed even if you set an error level.
But here is your answere: php.net
<?php
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
?>
with ini_set() during the execution of the script your options take action, after execution it is back to default.
Related
I can't turn off display errors for my website. I use ISP Manager control panel. It is Off in all php.ini files and settings. But still warnings and notices are showing. Even using ini_set() right in the script doesn't work. Can you help me with this?
<?php
ini_set('display_errors', 0);
phpinfo(); // display_errors is still On On
ini_get('display_errors'); // returns 'stderr'
If I understood correctly, something like:
var_dump(ini_set('display_errors', 0));
… produces:
bool(false)
I can think of two ways to intentionally prevent display_errors from being changed:
Disabling ini_set() altogether in system-wide INI file, which produces different symptoms:
Warning: ini_set() has been disabled for security reasons
NULL
This can be checked anyway:
var_dump(ini_get('disable_functions'));
Hard-coding display_errors in Apache settings using php_admin_flag, which only applies if PHP runs as Apache module but effectively produces a boolean false.
I believe we're in #2. You may want to check whether PHP runs as Apache module; I'm not aware though of any way to verify by yourself if php_admin_flag is being used. If that's the case, I reckon you're out of luck:
php_admin_flag name on|off
Used to set a boolean configuration directive. This can not be used in
.htaccess files. Any directive type set with php_admin_flag can
not be overridden by .htaccess or ini_set().
If you're in control of Apache settings this is something you can easily fix. Otherwise, I suggest you ask hosting support about this. IMHO, it isn't reasonable to enable display_errors in a production server, let alone force it:
; 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
Overview
There are a few different types of error reporting function in PHP. Luckily we have a decent explanation of these in the PHP docs here.
I typically use the three in the examples below. Let's walk through those.
Breakdown Docs
This function sets the error reporting level.
error_reporting()
The error_reporting() function sets the error_reporting directive at
runtime. PHP has many levels of errors, using this function sets that
level for the duration (runtime) of your script. If the optional level
is not set, error_reporting() will just return the current error
reporting level.
This first function takes a parameter of an integer or a named constant. The named constant is recommended in case future version of PHP release new error levels. That way you will always know what to expect after upgrading to a newer version of PHP.
This next mode decides if errors will be printed to the screen or not.
ini_set('display_errors', 1)
This determines whether errors should be printed to the screen as part
of the output or if they should be hidden from the user.
The last one handles errors that happen during PHP's startup sequence. When you turn display_errors on it does not handle errors that occur in the startup sequence. This is partially the reason why a lot of times people do not understand why they are not seeing errors even though they have turned error reporting on.
Even when display_errors is on, errors that occur during PHP's startup
sequence are not displayed. It's strongly recommended to keep
display_startup_errors off, except for debugging.
This will tell the app to log errors to the server.
ini_set('log_errors', 1);
Tells whether script error messages should be logged to the server's
error log or error_log. This option is thus server-specific.
Example
To turn off error reporting in a file try this,
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);
To turn on error reporting in a file try this,
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Answer
If you want to log errors but do not want them to show up on the screen then you would need to do this,
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
Or try,
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set("log_errors", 1);
error_reporting(E_ALL);
So what's going on is I tried
ini_set('display_errors', 'Off');
error_reporting(0);
Right below <?php, but this didn't seem to stop displaying them. So I went to the php.ini and went to display_errors and saw that it was set to Off. But it still showed.
So I went and did phpinfo() and display_errors along with display_startup_errors are both off. Also html_errors is off. I'm not sure if this will help, but it says error_reporting is set to -10241. Any ideas?
Do not change the value of error reporting to solve the issue. If display_errors is off, errors are not display independently of the error_reporting setting. This way you will not display errors but you can still log them.
The following should work:
ini_set('display_errors', 'Off');
If it doesn't work it could be that your server configuration does not allow you to change settings from PHP scripts. ini_set() returns FALSE on failure. So first of all you should check what value that call is returning. Make sure that ini_set is not listed among disabled PHP functions (disable_functions in php.ini).
If you are asking yourself why errors are still being displayed even if in php.ini the display_errors is Off, you can check the actual value of display_errors during the script execution:
ini_get('display_errors')
Pemember that PHP settings could be changed also in Apache host configuration and in .htaccess files. So check if you have an htacces that enables display_errors. Something like this:
php_flag display_errors on
Try to use:
ini_set('display_errors', 0);
ini_set('display_errors', false);
You don't describe what the errors are, so it's possible that your web server (Apache, nginx, etc) are what's throwing the error and not PHP.
If it is PHP, ensure that you're editing the correct php.ini as identified in your phpinfo.php. Remember that if you edit the php.ini, you will need to restart your PHP process (for example, on some *nix systems: service php-fpm restart. Your exact command may vary.)
If it's off in your php.ini, my guess is that it's being overridden somewhere else -- either later in the script ('grep "ini_set" /path/to/project/*.php' will find it). Also, the PHP Manual states that if the script has fatal errors, it doesn't apply if there are fatal errors:
Although display_errors may be set at runtime (with ini_set()), it
won't have any effect if the script has fatal errors. This is because
the desired runtime action does not get executed.
Every time I have an error within my code and try to run it, the page becomes inaccessible. This is clearly very frustrating as it's hard debugging code with no feedback.
Relevant information from cPanel:
Apache version 2.2.22
PHP version 5.3.14
MySQL version 5.1.68-cll
Architecture x86_64
Operating system linux
If more information is required then please ask, I'm sorry I cannot provide any more information but frankly I am stumped.
Thanks.
Enable error reporting to see what error PHP had, if it had one.
There are several places you can look, firstly try checking your Apache error log. In many cases this is located in /var/log/apache2/error.log . Another way to debug a page like this is to enable error logging.
The simplest way of doing this being adding these lines to your php file:
ini_set('display_errors',1);
error_reporting(E_ALL);
In addition to this, you can also clean up the errors formatting by adding:
ini_set('html_errors', 'On');
In addition to this method of enabling error reporting, you may also enable them from you configuration file by adding the following line:
error_reporting = E_ALL
You need to update your php.ini to display errors. There are a couple settings.
Search your php.ini for display_errors and error_reporting. The file is usually commented very well on the options for error reporting, but error_reporting = E_ALL is a typical setting. Sometimes people want to suppress notices and set error_reporting to E_ALL & ~E_NOTICE.
display_errors = On is the config to print the errors to the screen.
After changing your php.ini, Apache usually needs to be restarted. I'm not sure how much control you have over your server, so if you can't restart Apache but you have a php.ini available, your host probably has it configured so you don't need to restart.
I don't get any PHP error, just get a white page on Firefox, and
Server error
The website encountered an error while retrieving http://example.com/pruebas/prov.php. It may be down for maintenance or configured incorrectly
on Chrome.
This is the code:
if (!ini_get('display_errors')) {
ini_set('display_errors', 1);
}
echo "hola"
echo "hola2";
I intentionally made mistake in the echo "hola" (there's no ';').
I also tried adding to the end of my .htaccess file -> suPHP_ConfigPath /home/username/public_html replacing username with my current username, and then I created a php.ini in public_html with "display_errors = on;". But I'm still not able to get any PHP error.
Your script is dying due to the syntax error before it ever executes, so the ini_set() call is never executed and never takes effect. You'd have to change the setting in the appropriate php.ini.
The actual error message may be in a log file somewhere. Try Apache's error_log, or see if PHP's logging somewhere else.
Make sure that you also have the appropriate error_reporting ini value set as well. You can find more information on PHP.net
Set the error reporting level. The parameter is either an integer representing a bit field, or named constants. The error_reporting levels and constants are described in Predefined Constants, and in php.ini. To set at runtime, use the error_reporting() function. See also the display_errors directive.
In PHP 4 and PHP 5 the default value is E_ALL & ~E_NOTICE. This setting does not show E_NOTICE level errors. You may want to show them during development.
Source
Make sure the display_errors is set to on on your php.ini file.
I want to keep errors out of my PHP output stream. I only want output of things I explicitly echo.
Looking at my php.ini, is "display_errors" the only configuration I need to change?
Instead of modifying php.ini, you can call this at a very early part of your code:
error_reporting(0);
Note that this means fatal errors will die silently as well, so it makes it a little difficult to debug at first.
I only recommend that if we're talking about a production machine. display_errors will hide them from the user, but make sure you have log_errors and error_log set in the php.ini so you'll see them on your regular log analysis (you do, right?).
For a development machine, I recommend keeping display_errors on and error_reporting(E_ALL | E_STRICT) so you'll see if anything is fishy.
You can modify that INI directive and change the flag to 0 (False) or disable error_reporting on a page by page basis
error_reporting(0)
Typically Production environments should be on display_errors = 0 Though not all of us have both Development and Production environments
You can also change the "verbosity" of the error messages by passing different values to error_reporting function (Or by changing the INI value for it in php.ini) More information on that can be found here: PHP: Runetime Configuration - error_reporting