Php 为什么显示错误会更改HTTP状态代码?

Php 为什么显示错误会更改HTTP状态代码?,php,http,Php,Http,正如在许多其他问题中指出的那样,在php.ini中将display_errors设置为Off会使web服务器在遇到致命错误时使用状态代码500 Internal server error(内部服务器错误),而不是200 OK(正常)。我设置了一个带有未定义函数的简单测试来解释行为: php.ini display_errors = On index.php <?php test(); 或者只是一个空白页,如果我像这样使函数调用静音: <?php @test(); 将php.in

正如在许多其他问题中指出的那样,在php.ini中将display_errors设置为Off会使web服务器在遇到致命错误时使用状态代码500 Internal server error(内部服务器错误),而不是200 OK(正常)。我设置了一个带有未定义函数的简单测试来解释行为:

php.ini

display_errors = On
index.php

<?php test();
或者只是一个空白页,如果我像这样使函数调用静音:

<?php @test();
将php.ini更改为:

display_errors = Off
原因:

HTTP/1.0 500 Internal Server Error
Date: Tue, 10 Jul 2012 20:10:35 GMT
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.8
Content-Length: 0
Connection: close
Content-Type: text/html

有谁能解释一下,当display\u errors关闭时,web服务器响应500的底层机制吗?

原因是,使用
display\u errors=On
,您实际上是在要求PHP即使在脚本中有错误时也能给您一个体面的HTTP响应。将其视为脚本和响应之间的附加层。控制输出的不再是您的脚本,而是PHP

当您打开此选项时,实际上是在说,“如果有错误,请仍然给我一个有效的HTTP响应页面(它甚至可能包含适当的标记),因为我将查看而不是我的日志。”


如果将其设置为
关闭
,HTTP响应应该是无意义的,因此为500。打开它时,预计会出现PHP错误,因此请求总体上不是500,即使脚本失败。

在测试过程中,我确认一旦出现致命错误,它应该始终返回500。我也想知道为什么……重复:在我看来,这是一个不同的问题
display_errors = Off
HTTP/1.0 500 Internal Server Error
Date: Tue, 10 Jul 2012 20:10:35 GMT
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.8
Content-Length: 0
Connection: close
Content-Type: text/html