0

Related: Best way to send HTTP response code in PHP

Since I am implementing both client and server sides, I would like to define my own response codes, so are to give meaningful error messages to the user.

The PHP documentation in (currently) comment #48, but Stefan W, says:

Note that you can NOT set arbitrary response codes with this function, only those that are known to PHP (or the SAPI PHP is running on).

The following codes currently work as expected (with PHP running as Apache module): 200 – 208, 226 300 – 305, 307, 308 400 – 417, 422 – 424, 426, 428 – 429, 431 500 – 508, 510 – 511

Codes 0, 100, 101, and 102 will be sent as "200 OK".

Everything else will result in "500 Internal Server Error".

If you want to send responses with a freestyle status line, you need to use the header() function:

 <?php header("HTTP/1.0 418 I'm A Teapot"); ?>

That restricts the number of codes that I can send. Would I better using a generic code and a description error text which the client can present as-is to the user? (or two, one 4xx and one 5xx?)

Are there alternatives? I don't want this question to be closed, but will risk saying that I wax nostalgic for the days when we had a tag :-(

7
  • 1
    I don't think you should start inventing your own HTTP status codes willy-nilly. And I am not sure whether an error message intended for the end user, should be transported this way in the first place. Commented Sep 8, 2021 at 10:31
  • 2
    "That restricts the number of codes that I can send. " - that applies to http_response_code. As the user already mentioned, using header("HTTP/1.0 ...) would still allow you to respond with whatever status code you like. Commented Sep 8, 2021 at 10:32
  • Obviously it's just an opinion but there's a reason the REST approach to HTTP response codes is popular... Commented Sep 8, 2021 at 10:37
  • @CBroe Please remember that I am designing & implementing both server & client. The serve rise in the best position to know exactly what went wrong. If the client sends problematic data due to user input, should I just send a generic response (e.g 409 Conflict; but it doesn't have to be that one). That way the client can only say "invalid user input", and say which field is invalid. The client can't validate, because it is a database problem (already exists), known only to the server - or should I add APIs to verify each user input field individually? Commented Sep 8, 2021 at 11:46
  • 1
    "Please remember that I am designing & implementing both server & client." - that doesn't justify "playing fast & lose" with HTTP, in my opinion. (Maybe someone would want to switch out either side of this implementation for a "standard component" later on at some point.) If you need to provide more detailed error information, then why don't do what most APIs do - and put it into the response body? For example as a JSON data structure, that could tell the client what was wrong with which fields in as much details as you like. Commented Sep 8, 2021 at 12:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.