NotionCommotion Posted June 24, 2016 Share Posted June 24, 2016 (edited) http://php.net/manual/en/function.http-response-code.php will Get or Set the HTTP response code. Why would one ever need to do so? The documentation appears to just return an integer. Does it send headers as well? Guess that would be nice so I don't need to create them myself such as "HTTP/1.1 200 OK". I would like to leave the default at 200, but only do differently explicitly. But doesn't it change it for future? Edited June 24, 2016 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/301393-when-is-http_response_code-ever-needed/ Share on other sites More sharing options...
Jacques1 Posted June 24, 2016 Share Posted June 24, 2016 The function sets (or gets) the HTTP code of the upcoming response. It does not send any headers. The big advantage over setting the code manually with a header() call is that you can omit the HTTP version as well as the reason phrase (“OK”, “Not Found”, ...). PHP will figure those out for you. I use the function regularly, e. g. for error handling (issue a 400 code if the input validation failed). Quote Link to comment https://forums.phpfreaks.com/topic/301393-when-is-http_response_code-ever-needed/#findComment-1534004 Share on other sites More sharing options...
NotionCommotion Posted June 24, 2016 Author Share Posted June 24, 2016 So, I just do http_response_code(400); and call it good? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/301393-when-is-http_response_code-ever-needed/#findComment-1534005 Share on other sites More sharing options...
Solution Jacques1 Posted June 24, 2016 Solution Share Posted June 24, 2016 (edited) Yes. It does not somehow alter the PHP configuration to send a 400 response code forever – if that's what you're worried about. Only the pending response is affected. Edited June 24, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301393-when-is-http_response_code-ever-needed/#findComment-1534006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.