使用文件“php.ini”关闭显示错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15949304/
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
Turn off display errors using file "php.ini"
提问by Jms Bnd
I am trying to turn off all errors on my website. I have followed different tutorials on how to do this, but I keep getting read and open error messages. Is there something I am missing?
我正在尝试关闭我网站上的所有错误。我遵循了有关如何执行此操作的不同教程,但我一直在阅读和打开错误消息。有什么我想念的吗?
I have tried the following in my php.inifile:
我在我的php.ini文件中尝试了以下内容:
;Error display
display_startup_errors = Off
display_errors = Off
html_errors = Off
docref_root = 0
docref_ext = 0
For some reason when I do a fileopen() call for a file which does not exist, I still get the error displayed. This is not safe for a live website, for obvious reasons.
出于某种原因,当我对不存在的文件执行 fileopen() 调用时,我仍然显示错误。出于显而易见的原因,这对于实时网站来说是不安全的。
采纳答案by Sherlock
I always use something like this in a configuration file:
我总是在配置文件中使用这样的东西:
// Toggle this to change the setting
define('DEBUG', true);
// You want all errors to be triggered
error_reporting(E_ALL);
if(DEBUG == true)
{
// You're developing, so you want all errors to be shown
display_errors(true);
// Logging is usually overkill during development
log_errors(false);
}
else
{
// You don't want to display errors on a production environment
display_errors(false);
// You definitely want to log any occurring
log_errors(true);
}
This allows easy toggling between debug settings. You can improve this further by checking on which server the code is running (development, test, acceptance, and production) and change your settings accordingly.
这允许在调试设置之间轻松切换。您可以通过检查代码在哪个服务器上运行(开发、测试、验收和生产)并相应地更改设置来进一步改进这一点。
Note that no errors will be logged if error_reporting is set to 0, as cleverly remarked by Korri.
请注意,如果 error_reporting 设置为 0,则不会记录任何错误,正如 Korri 巧妙地指出的那样。
回答by Levite
You should consider not displayingyour error messages instead!
您应该考虑不显示错误消息!
Set ini_set('display_errors', 'Off');
in your PHP code (or directly into your ini file if possible), and leave error_reporting on E_ALL
or whatever kind of messages you would like to find in your logs.
设置ini_set('display_errors', 'Off');
在您的 PHP 代码中(如果可能,直接设置到您的 ini 文件中),并保留 error_reportingE_ALL
或您希望在日志中找到的任何类型的消息。
This way you can handle errors later, while your users still don't see them.
通过这种方式,您可以稍后处理错误,而您的用户仍然看不到它们。
Full example:
完整示例:
define('DEBUG', true);
error_reporting(E_ALL);
if (DEBUG)
{
ini_set('display_errors', 'On');
}
else
{
ini_set('display_errors', 'Off');
}
Or simply (same effect):
或者简单地(相同的效果):
define('DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', DEBUG ? 'On' : 'Off');
回答by Vishnu R
In php.ini
, comment out:
在php.ini
,注释掉:
error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
error_reporting = E_ALL & ~E_NOTICE
By placing a ;
ahead of it (i.e., like ;error_reporting = E_ALL & ~E_NOTICE
)
通过在它;
前面放置一个(即,像;error_reporting = E_ALL & ~E_NOTICE
)
For disabling in a single file, place error_reporting(0);
after opening a php
tag.
要在单个文件中禁用,请error_reporting(0);
在打开php
标签后放置。
回答by Shahzeb chohan
In file php.iniyou should try this for all errors:
在文件php.ini 中,您应该针对所有错误尝试此操作:
error_Reporting = off
回答by mario
Let me quickly summarize this for reference:
让我快速总结一下以供参考:
error_reporting()
adapts the currently active setting for the default error handler.Editing the error reporting ini optionsalso changes the defaults.
Here it's imperative to edit the correct
php.ini
version - it's typically/etc/php5/fpm/php.ini
on modern servers,/etc/php5/mod_php/php.ini
alternatively; while the CLI version has a distinct one.Alternatively you can use depending on SAPI:
- mod_php:
.htaccess
withphp_flag
options - FastCGI: commonly a local
php.ini
- And with PHP above 5.3 also a
.user.ini
- mod_php:
Restarting the webserver as usual.
error_reporting()
调整默认错误处理程序的当前活动设置。编辑错误报告 ini 选项也会更改默认值。
在这里必须编辑正确的
php.ini
版本 - 它通常/etc/php5/fpm/php.ini
在现代服务器上,/etc/php5/mod_php/php.ini
或者;而 CLI 版本有一个不同的版本。或者,您可以根据 SAPI 使用:
- mod_php:
.htaccess
带php_flag
选项 - FastCGI:通常是本地的
php.ini
- 并且 PHP 5.3 以上也是一个
.user.ini
- mod_php:
像往常一样重新启动网络服务器。
If your code is unwieldy and somehow resets these options elsewhere at runtime, then an alternative and quick way is to define a custom error handler that just slurps all notices/warnings/errors up:
如果您的代码笨拙,并且在运行时以某种方式在其他地方重置了这些选项,那么另一种快速的方法是定义一个自定义错误处理程序,它只是将所有通知/警告/错误都包含在内:
set_error_handler(function(){});
Again, this is not advisable, just an alternative.
同样,这不是可取的,只是一种替代方法。
回答by Ismael Fdez
In file php.iniyou should try this for all errors:
在文件php.ini 中,您应该针对所有错误尝试此操作:
display_errors = On
Location file is:
位置文件是:
- Ubuntu 16.04:/etc/php/7.0/apache2
- CentOS 7:/etc/php.ini
- Ubuntu 16.04:/etc/php/7.0/apache2
- CentOS 7:/etc/php.ini
回答by Tim S.
You can also use PHP's error_reporting();
你也可以使用 PHP 的 error_reporting();
// Disable it all for current call
error_reporting(0);
If you want to ignore errors from one function only, you can prepend a @
symbol.
如果只想忽略一个函数的错误,可以在前面添加一个@
符号。
@any_function(); // Errors are ignored
回答by AlexxanderX
Turn if off:
如果关闭则关闭:
You can use error_reporting();
or put an @in front of your fileopen().
您可以在 fileopen() 前面使用error_reporting();
或放置 @。
回答by Vladimir Ramik
I usually use PHP's built in error handlers that can handle every possible error outside of syntax and still render a nice 'Down for maintenance' page otherwise:
我通常使用 PHP 的内置错误处理程序,它可以处理语法之外的所有可能的错误,并且仍然呈现一个不错的“停机维护”页面,否则:
回答by UWU_SANDUN
Open your php.ini file (If you are using Linux - sudo vim /etc/php5/apache2/php.ini)
打开您的 php.ini 文件(如果您使用的是 Linux - sudo vim /etc/php5/apache2/php.ini)
Add this lines into that file
将此行添加到该文件中
error_reporting = E_ALL & ~E_WARNING
(If you need to disabled any other errors -> error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING)
(如果您需要禁用任何其他错误 -> error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING)
display_errors = On
And finally you need to restart your APACHE server.
最后,您需要重新启动 APACHE 服务器。