简体   繁体   中英

HTTP Status Codes in a PHP Application

I recently decided to send proper HTTP header from my PHP application.
Since I've never done that before, I have a couple of very basic questions.

Status codes 200 and 404 seem obvious.
But how about the other ones?

Is it allowed to/recommended/not recommended to use 403 for example, if the user has to log in via a HTML form in order to use the page?

How about 415 for file uploads? Would it be a good idea to set a 415 header if the user uploads an image with a non-supported extension, for example?

Status 409 (conflict) can be returned for double submissions, status 401 for unauthorized submissions. For bad requests (unprocessable) you can return status 422. Here and here there are more suggestions on HTTP status code.

The full list of W3C recognised codes is here . The definitions contain clear guidance on when each code should and should not be used. Your suggested use of 403, for example, is at odds with the W3C definition.

It depends on application you creating.

If you creating restfull app Its mandatory to use proper status codes. But when you creating backend generated web site Its up to you how you handle client/server errors and routing.

I think its only one rule to do -for a good user experience - handle all errors, the proper way (if 401 then redirect to login etc.)

I think you should use these status codes for a typical website:

  • 200 Ok (if everything is fine, whats even true if someone does a corrupted file upload - give an error message to the user)
  • 301 Permanent redirect
  • 302 temporally redirect
  • 304 Not modified
  • 404 Not found
  • 410 Gone

If you're building a REST webservice, you can use some more status codes. But for a typical website the status codes listed above are fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM