为什么display_errors会更改HTTP状态代码? [英] Why does display_errors change the HTTP status code?

查看:40
本文介绍了为什么display_errors会更改HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As pointed out in many other questions, turning display_errors to Off in php.ini makes the web server answer with status code 500 Internal server error instead of 200 OK when encountering a fatal error. I set up a simple test with an undefined function to explain the behaviour:

php.ini

display_errors = On

index.php

index.php

<?php test();

赠予:

Fatal error: Call to undefined function test()
in D:\xampp\htdocs\index.php on line 1

或者如果我使函数调用保持沉默,则只是空白页:

or just a blank page if i silence the function call like this:

<?php @test();

在两种情况下,答案标题都如下:

In both cases answer headers are as following:

HTTP/1.1 200 OK
Date: Tue, 10 Jul 2012 20:08:22 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
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

将php.ini更改为:

While changing php.ini to:

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_errors关闭时使Web服务器以500应答的底层机制吗?

Can anybody explain me the underlaying mechanism that makes the web server answer with 500 when display_errors is Off?

推荐答案

原因是使用 display_errors = On 时,您实际上是在要求PHP为您提供体面的HTTP响应,即使出现错误也是如此在您的脚本中.可以将其视为脚本和响应之间的附加层.它不再是您的脚本来控制输出,而是PHP.

The reason is that with display_errors = On, you are essentially asking PHP to give you a decent HTTP response even when there are errors in your script. Think of it like an additional layer between your script and the response. It's no longer your script controlling the output, it's PHP.

当您打开此选项时,实际上是在说:"如果出现错误,请仍然给我一个有效的HTTP响应页面(它甚至可能包含不错的标记),因为我会关注那个,而不是我的日志."

When you turn this option on, you are in effect saying, "If there is an error, please still give me a valid HTTP response page (it may even include decent markup), since I will be looking at that instead of my logs."

如果将其设置为 Off ,则HTTP响应应该是无意义的,因此应为500.如果启用,则预期会出现PHP错误,因此请求总体上 >不是500,即使您的脚本失败了.

With it set to Off, the HTTP response should be meaningless, and therefore a 500. With it On, a PHP error is anticipated, so the request on the whole is not 500, even though your script failed.

这篇关于为什么display_errors会更改HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆