Stop printing php error messages to browser

2020-03-12 04:00发布

问题:

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

回答1:

display_errors = Off

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



回答2:

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


回答3:

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



回答4:

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



回答5:

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


回答6:

See display_errors directive

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



回答7:

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



回答8:

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