如何避免或限制PHP错误输出

时间:2013-10-03 06:22:05

标签: php error-handling

由于极慢的连接速度等原因,我的PHP网络应用有时会返回此Fatal error on Line XXX。虽然,这种情况很少发生,例如1000次中的1次,我想在浏览器上避免这样的输出。我正在服务器端进行错误记录以获取某些功能。所以,我不想完全禁用错误报告。只是我不想将它显示给最终用户。

5 个答案:

答案 0 :(得分:3)

关闭error_reporting的各种示例..

<?php

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>

注意:您可以将include()放入任何您不希望显式错误的文件中。或者,如果你想完全取消它。在php.ini文件中进行调整。

在你的php.ini中找到要调整和关闭error_reporting的行。

 ini_set('display_errors', 'On'); //change to Off
        error_reporting(E_ALL); // change to 0

进一步信息:: PHP.NET

答案 1 :(得分:2)

要禁止向页面用户显示任何错误,请设置

display_errors = Off

在你的php.ini中(无论如何这是生产网站的推荐设置!)或

ini_set('display_errors', 0);

在你的PHP中。

display_errors设置仅影响网页上的输出;它不会影响可能配置的日志文件;那里的消息仍然会被记录下来。

请参阅有关配置错误报告的php文档:http://php.net/manual/en/errorfunc.configuration.php

注意:据我所知,此处其他用户提到的error_reporting设置会影响所有类型的错误报告(也就是报告给可能配置的日志文件的内容) 。如果将error_reporting设置为0,则不会获得任何日志条目。如果您想将某些内容记录到日志文件中但不向用户显示,则可以使用display_errors设置。

答案 2 :(得分:1)

在该页面的开头写

  error_reporting(0);

答案 3 :(得分:0)

为避免此类问题,您可以设置 max_execution_time = 1024M 以避免生成慢速数据,并且在php.ini文件中隐藏错误的次数必须是error_reporting = 0。
或者干脆:

PHP Code
ini_set('max_execution_time',1024M);
error_reporting(0);

PHP Ini Settings
max_execution_time=1024M
error_reporting=0

希望它有所帮助。

答案 4 :(得分:0)

的error_reporting(0);仅在您未使用set_error_handler(&#39; my_function&#39;)时才有效。如果是这种情况,您可以在&#39; my_function&#39;中取消错误消息。