PHP中error_reporting()函数的用法(修改PHP屏蔽错误)

2019-04-09 03:19:45王冬梅

    error_reporting = E_ALL & ~E_NOTICE

  重新启动 Apache,就全部设置好了。接下来,将学习如何在 Apache 上做同样的事。

  服务器上的错误报告
  依赖于 Apache 正在做的工作,在 PHP 中打开错误报告可能没法工作,因为在计算机上可能有多个 PHP 版本。有时很难区分 Apache 正在使用哪个 PHP 版本,因为 Apache 只能查看一个 php.ini 文件。不知道 Apache 正在使用哪个 php.ini 文件配置自己是一个安全问题。但是,有一种方法可以在 Apache 中配置 PHP 变量,从而保证设置了正确的出错级别。

  而且,最好知道如何在服务器端设置这些配置变量,以否决或抢占 php.ini 文件,从而提供更高级别的安全性。
在配置 Apache 时,应该已经接触过 /conf/httpd.conf 中 http.conf 文件中的基本配置。

  要做在php.ini文件中已经做过的事,请把下列各行添加到 httpd.conf,覆盖任何 php.ini 文件:
    php_flag display_errors on
    php_value error_reporting 2039
  这会覆盖在 php.ini 文件中为 display_errors 已经设置的标志,以及 error_reporting 的值。值 2039 代表 E_ALL & ~E_NOTICE。如果愿意采用 E_ALL,请把值设为 2047。同样,还是要重启 Apache。
  接下来,要在服务器上测试错误报告。

关于error_reporting()这个函数,它是可以屏蔽到一些错误信息,但是PHP 核心造成的错误,是无法屏蔽的,因为PHP 核心造成的错误会直接导致PHP文件编译失败,因为书写格式没有按照PHP的编码规则写而造成的错误,是无法屏蔽的

* For now, avoid warnings of E_STRICT mode
* (this must be done before function definitions)
*/
if (defined('E_STRICT')) {
$old_error_reporting = error_reporting(0);
if ($old_error_reporting & E_STRICT) {
error_reporting($old_error_reporting ^ E_STRICT);
} else {
error_reporting($old_error_reporting);
}
unset($old_error_reporting);

常见的如下:

// 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;报告所有的错误但不包括E_NOTICE 这也是php.ini的缺省设置
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (bitwise 63 may be used in PHP 3);报告所有的错误
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);同上
ini_set('error_reporting', E_ALL);
相关文章 大家在看