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 best-practise tag :-(
http_response_code. As the user already mentioned, usingheader("HTTP/1.0 ...)would still allow you to respond with whatever status code you like.