Why does display_errors change the HTTP status code?

As pointed out in many other questions, turning display_errors to Off in php.ini causes the web server to respond with a status code of 500 Internal server error instead of 200 OK when a fatal error occurs. I installed a simple test with the undefined function to explain the behavior:

php.ini

display_errors = On 

index.php

 <?php test(); 

gives:

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

or just a blank page if I disable the function call as follows:

 <?php @test(); 

In both cases, the response headers are as follows:

 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 

When changing php.ini to:

 display_errors = Off 

Causes:

 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 

Can someone explain to me the lining mechanism that makes the web server respond with 500 when display_errors is off?

+6
source share
1 answer

The reason is that with display_errors = On you are essentially asking PHP to give you a decent HTTP response, even if there are errors in your script. Think of it as an extra layer between your script and the answer. This is not your script output control anymore, this is PHP.

When you enable this option, you actually say: β€œIf there is an error, please give me a valid HTTP response page (it may even include decent markup), since I will look at what instead of my logs.

When set to Off HTTP response should be meaningless and therefore 500. With it, a PHP error is expected, so the request as a whole is not 500, even if your script failed.

+6
source

Source: https://habr.com/ru/post/920162/


All Articles