Short question: Can I rely on the behaviour of header('.', TRUE, 404);
, header('.', TRUE, 501);
, etc.?
Now, let me elaborate.
Before asking my question, I'll present a few usages of the header()
call and the corresponding HTTP response code generated by it.
header('HTTP/1.1 404 Bummer!');
-- HTTP/1.1 404 Bummer!
header('HTTP/1.1 501 I am sick');
-- HTTP/1.1 501 I am sick
header('Bummer!', TRUE, 404);
-- HTTP/1.1 404 Not Found
header('I am sick', TRUE, 501);
-- HTTP/1.1 501 Method Not Implemented
So, we can see that if we use the third parameter while the first parameter is junk, the first parameter is ignored. However, the documentation at http://php.net/manual/en/function.header.php says:
Note that this parameter only has an effect if the string is not empty.
So, we still have to put something in the first parameter. This looks a bit ugly to me because the $string
is ignored when we specify $http_response_code
in the third parameter but we are still required to put some value for $string
even though it will never be used.
But I can understand why this turned out this way. Traditionally, header()
accepted only parameter and we could set arbitrary response codes like in the first two examples. The second and third parameters were added later as optional paramters. So, we have to specify something for the first parameter if we want to use the second and third. Moreover, sometimes we may need to put valid headers in the first parameter along with a valid response code in the third parameter. I have included one such example in the end.
So, I'm planning to use this function in this manner in my code: header('.', TRUE, 404);
, header('.', TRUE, 501);
, etc. As per the examples above, it'll produce correct HTTP responses as per the standard. I want to know if I can rely on this behaviour. I ask this question because I can't find the documentation mentioning explicitly that the first parameter ($string
) is going to be ignored when we provide the third ($http_response_code
).
BTW, I know that the first argument can be useful in situations like this.
header('Foo: Bar', TRUE, 501);
leads to:
HTTP/1.1 501 Method Not Implemented
Date: Sun, 09 Oct 2011 19:01:19 GMT
Server: Apache/2.2.20 (Debian)
X-Powered-By: PHP/5.3.8-1
Foo: Bar
Vary: Accept-Encoding
Connection: close
Content-Type: text/html
A more practical example is header('Location: http://example.com/there/', TRUE, 307);
which would generate:
HTTP/1.1 307 Temporary Redirect
Date: Sun, 09 Oct 2011 19:09:29 GMT
Server: Apache/2.2.20 (Debian)
X-Powered-By: PHP/5.3.8-1
Location: http://example.com/there/
Vary: Accept-Encoding
Content-Type: text/html
Anyway, back to my question. Can I rely on the behaviour of header('.', TRUE, 404);
, header('.', TRUE, 501);
, etc.?