display_errors & error_reporting(php调试安全)

时间:2022-11-29 22:27:40

这两个选项对应的值即都可以在php.ini文件中设置,也都可以在php代码中使用相应的函数来设置。

在php.ini文件中的设置如下:

display_errors & error_reporting(php调试安全)

Display_errors = On : 打开错误回显。

Display_errors = Off:关闭错误回显。

display_errors & error_reporting(php调试安全)

Error_reporting = ,如下:

常量

描述

1

E_ERROR

Fatal run-time errors. Errors that can not be recovered from. Execution of the script is halted

2

E_WARNING

Non-fatal run-time errors. Execution of the script is not halted

4

E_PARSE

Compile-time parse errors. Parse errors should only be generated by the parser

8

E_NOTICE

Run-time notices. The script found something that might be an error, but could also happen when running a script normally

16

E_CORE_ERROR

Fatal errors at PHP startup. This is like an E_ERROR in the PHP core

32

E_CORE_WARNING

Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core

64

E_COMPILE_ERROR

Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine

128

E_COMPILE_WARNING

Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine

256

E_USER_ERROR

Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()

512

E_USER_WARNING

Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()

1024

E_USER_NOTICE

User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()

2048

E_STRICT

Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code

4096

E_RECOVERABLE_ERROR

Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())

8191

E_ALL

All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

如有多个选项值,请使用 | 隔开。

在php代码中的设置如下:

Display_errors选项的设置使用ini_set函数。

Error_reporting选项的设置使用error_reporting函数。

代码如下:

例程1:

<?php

//获取并输出php.ini里display_errors变量的值

echo ini_get('display_errors');

echo '------------1-1-1------------';

echo '<br />';

//如果display_errors = 0(即Off),则重置display_errors = 1(即On)

if (!ini_get('display_errors'))

{

  ini_set('display_errors', '1');

}

echo ini_get('display_errors');

echo '<br />';

//获取并输出php.ini里所有变量的值

$ini = ini_get_all();

print_r($ini);

?>

例程2:

<?php

//禁用错误报告

error_reporting(0);

//报告运行时错误

error_reporting(E_ERROR | E_WARNING | E_PARSE);

//报告所有错误

error_reporting(E_ALL);

?>

例程3:

<?php

//记录生产环境错误日志

error_reporting(E_ALL);

ini_set('display_errors',0);

ini_set('log_errors',1);

ini_set('error_log','E:\'.date('Y-m-d').'_phpddt.com.txt');

?>

好了,php语言的安全特性到此就讲完了。