在生产服务器上抑制 php 错误的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/332178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to suppress php errors on production servers
提问by Matt
What is the best method of hiding php errors from being displayed on the browser?
隐藏 php 错误显示在浏览器上的最佳方法是什么?
Would it be to use the following:
是否会使用以下内容:
ini_set("display_errors", 1);
Any best practice tips would be appreciated as well!
任何最佳实践技巧也将不胜感激!
I am logging the errors, I just want to make sure that setting the display_errors value to off (or 0) will not prevent errors from being logged.
我正在记录错误,我只是想确保将 display_errors 值设置为关闭(或 0)不会阻止记录错误。
回答by Vinko Vrsalovic
The best way is to log your errors instead of displaying or ignoring them.
最好的方法是记录您的错误,而不是显示或忽略它们。
This example will log the errors to syslog instead of displaying them in the browser.
此示例将错误记录到 syslog 而不是在浏览器中显示它们。
ini_set("display_errors", 0);
ini_set("log_errors", 1);
//Define where do you want the log to go, syslog or a file of your liking with
ini_set("error_log", "syslog"); // or ini_set("error_log", "/path/to/syslog/file");
回答by grepsedawk
Assuming you are in control of the php.ini file, you can make these changes globally inside that file instead of having the ini_set code laying around in all your php files (which you might forget to put in one of your files one day, which could be bad in production).
假设您控制 php.ini 文件,您可以在该文件中全局进行这些更改,而不是将 ini_set 代码放置在所有 php 文件中(有一天您可能会忘记将其中一个文件放入其中,在生产中可能会很糟糕)。
回答by farzad
set an environment variable on your development machine. then you can check your environment in the beginning of your code to make sure you are on the development environment. then you can set the error reporting level of PHP, by using the error_reporting() function.
在您的开发机器上设置一个环境变量。那么您可以在代码的开头检查您的环境,以确保您处于开发环境中。然后您可以使用 error_reporting() 函数设置 PHP 的错误报告级别。
if ( isset($_ENV['MY_APP_MODE']) && ($_ENV['MY_APP_MODE'] == 'devel') ) {
error_reporting(E_ALL);
} else {
error_reporting(0);
}
回答by staticsan
The PHP distribution files unfortunately sends a mixed message about handling error messages. The default php.ini file has some settings suitable for production servers and others better for development servers. You should turn all errors on in dev (error_reporting = E_ALLand display_errors = On), and off in prod (display_errors = offbut point log_errorssomewhere). However, do your best to make sure there are no errors (or they're handled).
不幸的是,PHP 分发文件发送了关于处理错误消息的混合消息。默认的 php.ini 文件有一些适合生产服务器的设置,而另一些则更适合开发服务器。您应该在 dev (error_reporting = E_ALL和display_errors = On)中打开所有错误,并在 prod 中关闭所有错误(display_errors = off但要指向log_errors某处)。但是,请尽力确保没有错误(或已处理)。
回答by Luis Melgratti
You can also use apache .htaccess to log errors without touching your code:
您还可以使用 apache .htaccess 来记录错误而无需触及您的代码:
<IfModule mod_php5.c>
php_flag display_errors Off
php_flag log_errors On
php_value error_log logs/errors
</IfModule>
回答by Magnus W
The accepted answer did not work at all for me on PHP 7.1.
在 PHP 7.1 上,接受的答案对我来说根本不起作用。
This works as expected on production, logging errors to the supplied file and not displaying them to the user:
这在生产中按预期工作,将错误记录到提供的文件中,而不向用户显示:
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
ini_set("error_log", "/absolute/path/to/my/error_log");
回答by Magnus W
The best way? Fix the errors!
最好的方法?修正错误!
If you need a quick temporary solution then it's OK to turn display_errors off, but make sure you're logging them somewhere, because that setting will make serious/parse errors silent otherwise.
如果您需要一个快速的临时解决方案,那么关闭 display_errors 是可以的,但请确保您将它们记录在某处,因为否则该设置将使严重/解析错误静音。

