I'm using PHP 5.3, CentOS 6.2, httpd 2.2.15, NetBeans 7.0.1 (running remotely via ftp).
I want to stop printing error messages to the browser, it's enough that it prints to the error_log of httpd.
I thought by doing try/catch I would decide on my own how to handle the error but it still prints to both error_log and browser.
function smic_gettext($phrase){
try{
$tr_text = $this->language_array[$phrase];
} catch(Exception $e){
error_log("Couldn't find any entry in the translation file for ".$phrase.". ".$e);
return $phrase;
}
return $tr_text;
}
How should I configure in order to stop this behaviour?
I have tried setting display_errors=Off and display_errors=0 in php.ini. No difference (I did restart httpd).
display_errors = Off
in php.ini will let you keep your syslog errors, but write nothing to the browser.
You need to change the php.ini setting display_errors to off or 0. You can either do this in your actual php.ini, with a .htaccess file, or by calling this at the start of the script:
ini_set('display_errors', '0');
Try adding the following to the top of your script:
ini_set('display_errors',0);
This should set the error reporting to none and override the servers php.ini settings (which sometimes ignore your error_reporting(0) )
Wheter or not PHP errors are sent to the browser is determined by the php.ini setting: display_errors. Set it to Off to avoid it being output. This file is usually located under /etc/php.ini or /etc/php5/php.ini
If error appears only in one line it is possible to prevent error display with adding sign # to start of that line.
#YOUR_CUSTOM_COMMAND
Example:
#file_get_contents('custom_file.txt');
See display_errors directive
http://www.php.net/manual/en/errorfunc.configuration.php
If you want to hide errors and warnings, you can also set an error_handler.
See http://php.net/manual/function.set-error-handler.php
FWIW, while display_errors = off is the correct config-file line as others have said, on DreamHost (nd possibly other installations), it goes in
$HOME/.php/phprc
rather than php.ini (which might also work, but DreamHost -- and, again, possibly others -- supports phprc).
Related
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.
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"
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 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 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.