13

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

Nicsoft
  • 3,644
  • 9
  • 41
  • 70
  • **production environments** should have PHP errors/notices disabled in the `php.ini` file, and only logging to log files – Jakub Jan 19 '12 at 14:52
  • Side note: Errors in PHP are not exceptions (like in Java for example) but there is a way to convert them to exceptions and handle accordingly. See http://www.php.net/manual/en/class.errorexception.php – Mchl Jan 19 '12 at 15:01

8 Answers8

13

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');
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
13
display_errors = Off

in php.ini will let you keep your syslog errors, but write nothing to the browser.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • I would like to do this for all scripts so configuring php.ini seems to be the best way. I did try display_error=Off and display_error=0, restarting httpd but no difference. – Nicsoft Jan 19 '12 at 15:17
  • You tried "display_error", but the keyword is "display_errors" – Eugen Rieck Jan 19 '12 at 15:22
  • Sorry about that, it wast just a typo in my comment, I used display_errors. – Nicsoft Jan 19 '12 at 15:27
  • 1
    Have you checked, that there is no other occurrence of "display_errors" in the file? – Eugen Rieck Jan 19 '12 at 15:29
  • 1
    This was extremely embarrassing... There was duplicate entries of display_errors. Guess it's time for a break. Thanks a lot! – Nicsoft Jan 19 '12 at 15:36
8

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) )

AlexC
  • 1,091
  • 13
  • 25
  • setting error_reporting to 0 will also cause no errors to be logged. I wouldn't advise this in a production environment. Setting `ini_set('display_errors',0);` should be enough. – klennepette Jan 19 '12 at 14:56
  • Mchl and klennepette, thanks for the assist. Nicsoft, they are right, just ini_set('display_errors',0); should do the job. – AlexC Jan 19 '12 at 15:01
7

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

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • @DaveRandom OP mentioned he is on CentOS and on CentOS this is usually the path. – Oldskool Jan 19 '12 at 14:53
  • I would like to do this for all scripts so configuring php.ini seems to be the best way. I did try display_error=Off and display_error=0, restarting httpd but no difference. – Nicsoft Jan 19 '12 at 15:17
  • It should be display_errors (plural), not error. If that's just a typo and display_errors is set to Off, make sure that there is no "overruling" display_errors in your script (e.g. with the ini_set method suggested by others) and there is nothing like `php_flag display_errors Off` in your .htaccess file (if you have one). – Oldskool Jan 19 '12 at 15:20
  • I actually used plural, it was just in my comment I made it singular (can't update it anymore). I am not using any .htaccess file. – Nicsoft Jan 19 '12 at 15:25
2

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');
Nole
  • 796
  • 7
  • 11
1

See display_errors directive

http://www.php.net/manual/en/errorfunc.configuration.php

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • I would like to do this for all scripts so configuring php.ini seems to be the best way. I did try display_errors=Off and display_errors=0, restarting httpd but no difference. – Nicsoft Jan 19 '12 at 15:19
0

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

Olie
  • 24,597
  • 18
  • 99
  • 131
0

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

freytag
  • 4,769
  • 2
  • 27
  • 32