Given the following two methods for setting a HTTP response code in PHP (specifically, under Apache):
Method 1:
http_response_code(404);
Method 2:
header("HTTP/1.0 404 Not Found");
My questions are:
http_response_code
is only available in PHP 5.4 or greater, what are the differences between the two approaches and why/when to use one over the other?For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-case which can detect a HTTP response line and replace that with a custom one. header( "HTTP/1.1 404 Not Found" );
Methods to Set HTTP Status Code Sr.No. This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter.
The http_response_code() function sets or returns the HTTP response status code.
Response response = client . target( url ) . request() . get(); // Looking if response is "200", "201" or "202", for example: if( Arrays.
To set a certain PHP HTTP response, you should use http_response_code () function. Now, there are various ways to control these responses. Let's see them in a table below:
Do not mix the use of http_response_code () and manually setting the response code header because the actual HTTP status code being returned by the web server may not end up as expected. http_response_code () does not work if the response code has previously been set using the header () function. Example: I only tested it on Apache.
You can't create your own response codes using this method, however you can using the header method. 1. Using http_response_code will cause PHP to match and apply a Reason Phrase from a list of Reason Phrases that are hard-coded into the PHP source code.
HTTP PHP functions allow you to manipulate PHP HTTP response (the information sent by your browser to server) before outputting it. These functions are inbuilt into PHP core. As we have already covered, exchanging data on the Internet consists of one side (client) asking for specific data and the other (server) supplying the requested information.
Since I'm being downvoted into oblivion for no apparent reason, I've managed to answer this myself by scouring through the PHP source code. Hopefully this serves as a reference for anyone else trying to work this out.
The two methods are essentially functionally equivalent. http_response_code
is basically a shorthand way of writing a http status header, with the added bonus that PHP will work out a suitable Reason Phrase to provide by matching your response code to one of the values in an enumeration it maintains within php-src/main/http_status_codes.h.
Note that this means your response code must match a response code that PHP knows about. You can't create your own response codes using this method, however you can using the header
method. Note also that http_response_code
is only available in PHP 5.4.0 and higher.
In summary - The differences between http_response_code
and header
for setting response codes:
Using http_response_code
will cause PHP to match and apply a Reason Phrase from a list of Reason Phrases that are hard-coded into the PHP source code.
Because of point 1 above, if you use http_response_code
you must set a code that PHP knows about. You can't set your own custom code, however you can set a custom code (and Reason Phrase) if you use the header
function.
http_response_code
is only available in PHP 5.4.0 and higher
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With