php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #18824 PHP should produce an appropriate status code when an error is encountered
Submitted: 2002-08-09 02:09 UTC Modified: 2002-08-09 05:40 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: bugs dot php dot net at mnot dot net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.2.2 OS: unknown (maybe FreeBSD?)
Private report: No CVE-ID: None
 [2002-08-09 02:09 UTC] bugs dot php dot net at mnot dot net
I don't use PHP, I'm just a protocol weenie.

I encountered the following from travel.yahoo.com tonight:

  HTTP/1.1 200 OK
  Date: Fri, 09 Aug 2002 06:02;01 GMT
  Connection: close
  Content-Type: text/html

  <br />
  <b>Fatal error</b>: Cannot instantiate non-existent   class: travel in <b>/home/y/share/htdocs/t/index.php</b> on line <b>19</b><br />

Fine, except that it gives a 200 OK status. The right thing would be to generate a 500 Server Error status; this indicates that something is wrong in a machine-understandable way, which is good. It shows up in the logfiles. Agents (like Google) understand that all is not well. 

Please modify PHP to generate appropriate status codes. 500 is just one example; have a look at RFC2616 for other status codes that you might want to map machine-understandable semantics to.

Cheers,

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-09 02:15 UTC] rasmus@php.net
You are assuming that PHP buffers all output before sending anything.  That is an incorrect assumption.  A fatal script error can occur long after the HTTP response headers have been sent.
 [2002-08-09 02:31 UTC] bugs dot php dot net at mnot dot net
So is the correct status code output if the response is buffered? AFAIK a fair number of PHP applications use output buffering these days, so it would be useful to take advantage.
 [2002-08-09 02:55 UTC] rasmus@php.net
No, buffering is not that common.  But even so, having the http status change depending on buffering would be quite inconsistent and would likely confuse the heck out of people.  Also, consider that errors generally occur after some valid content has been generated, and not sending this in a valid response would make it considerably harder to debug things.  

If someone wants this functionality in buffered mode, they could easily do it with their own error handling function.
 [2002-08-09 03:01 UTC] bugs dot php dot net at mnot dot net
IMP buffers, as does any other application using HTTP:Compress.

> Also, consider that errors generally occur after some 
> valid content has been generated, and not sending this 
> in a valid response would make it considerably harder to 
> debug things.  

A 500 response can have a body just as easily as a 200 can.

> If someone wants this functionality in buffered mode, 
> they could easily do it with their own error handling 
> function.

It's not the person writing the application who would necessarily want this functionality; it's the consumer of the application. The idea is to be compatible with the Web architecture, and that means honoring the semantics of the protocols.

Cheers,
 [2002-08-09 03:42 UTC] rasmus@php.net
I still don't agree that this should be a default thing.  An application level error is quite different from a server-level error.  Most production sites should be redirecting all errors to a log file or some other mechanism.  There can be runtime errors that occur that do not significantly impact users.  In that sense I do not believe application level errors should be extended to the client.  Perhaps an argument can be made for fatal php initialization errors that occur at the interface between the server and PHP.

For example, how about a simple redirection script which sends a location header and either a 301 or a 302.  Some logging code after the redirect fails for some reason.  In normal operation, the user still gets redirected, but in your scenario this becomes a 500 and the script no longer works.  
 [2002-08-09 03:49 UTC] bugs dot php dot net at mnot dot net
Please see Roy Fieldings' dissertation:
  http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
And comments by Henrik Frystyk Neilsen:
  http://discuss.develop.com/archives/wa.exe?A2=ind0004&L=soap&F=&S=&P=33153

Basically, there is no "Application Layer" vs. "Server Layer"; HTTP is an application layer protocol, and any differentiation between the software that serves the request and the software that implements the logic to generate the content is in the mind of the developer only. 

This point has been debated elsewhere ad nauseum, but the people who architected the Web in the first place all agree that HTTP status codes should carry this kind of information.
 [2002-08-09 04:09 UTC] rasmus@php.net
That's fine, and trust me, I have read Roy's papers, but you nicely avoided the realworld example I gave you which showed a perfect case where the application author needs control over when to make something a server error and when not to.  I fully agree that people should write decent error handling functions that do something intelligent, but I do not believe in removing flexibility from PHP and forcing every error to generate a 500.  
 [2002-08-09 04:55 UTC] bugs dot php dot net at mnot dot net
No, actually I missed it ;)

All that I'm proposing is that the default status code be changed from 200 to 500 *if* a fault is encoutered. If the script has changed it in the meantime, so be it. I definately don't want to remove the flexibility to express semantics in the language; only create the proper defaults based on known conditions.

I can think of many real-world situations where the current behaviour is broken; e.g., CDNs like Akamai will cache erroneous content if it comes with a 200, but do the right thing if they see a 500.

Tis late (at least here). Obviously, you don't agree with my arguments here, so I'm happy to leave this as is, for consideration by others.

Cheers,
 [2002-08-09 05:40 UTC] markonen@php.net
There is one huge, practical problem with the 500 responses -- IE/Win's canned error messages. Most people have not turned them off; many don't even know how. Getting the PHP error messages replaced by an obfuscated Microsoft notice will have a very, very high WTF factor for a lot of users.

That said, there is no doubt that a tutorial like "HTTP for PHP Developers" would be a very useful part of the PHP manual. Status codes and, for example, the IMS negotiation mechanism are among the worst understood aspects of this trade.
 [2002-08-09 17:16 UTC] asynch_messaging at hotmail dot com
"For example, how about a simple redirection script which sends a location header and either a 301 or a 302.  Some logging code after the redirect fails for some reason.  In normal operation, the user still gets redirected, but in your scenario this becomes a 500 and the script no longer works."

Good point. 
A more rigorous implementation would understand that 'after the redirect' means the response is complete and further errors are not HTTP level errors and so should not modify the response - status, headers or other bits.

It is unreasonable to expect the world to change to recognize these subtle points - either server frameworks or authored scripts - but knowing the issue exists can help future implementations of both.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 02:01:30 2024 UTC