1

I saw this header(X-PHP-Response-Code) here in this SO answer and was wondering.

  1. What is the function and are there any other X-PHP-* headers?
  2. If there are what is their function and usage (as much info as possible)?
  3. If this is the only one (in which I doubt) ignore 2.
Community
  • 1
  • 1
DaGhostman Dimitrov
  • 1,608
  • 20
  • 44

2 Answers2

2

In the example given, the header does exactly nothing, which is exactly the point.

HTTP headers starting with X- are precisely not standardized, so anyone can set such a header knowing that it doesn't mean anything specific. The point in the case you cite is simply to use the header function to set the response code. To do that, some header needs to be supplied. The user chose to use the otherwise meaningless header X-PHP-Response-Code, simply to be able to give some header to header() in order to use its third argument to set the response code.

In other words: it's a hack.

deceze
  • 510,633
  • 85
  • 743
  • 889
2

Headers starting with X- are not standard and their interpretation is entirely dependent on a client that looks for them and knows what they are supposed to mean.

This particular header does not have any meaning, it's just placeholder text to satisfy an extra requirement of the header function when a third argument http_response_code is supplied:

http_response_code

Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the string is not empty.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • It's so unuseful that the manual does not state whether the specified header string gets sent when you pass the `http_response_code` int. – CodeCaster Aug 13 '13 at 08:40
  • 1
    @CodeCaster: Well, `header`'s main job is to send header strings and the response code argument is optional. It may not say so explicitly, but it's not hard to connect the dots. :-) – Jon Aug 13 '13 at 08:48
  • It's not that easy. The `header(string, bool, http_response_code)` overload sends the special `status-line` of an HTTP response: `HTTP/1.1 $http_response_code $textual_representation`. If you **don't** specify the response code in the `header()` call, a **new** header gets sent with the `string` contents. I'm just wondering what happens to the `string` when you **do** specify the response code, given nothing happens if you don't. I guess it gets ignored, but the manual is silent on that point. – CodeCaster Aug 13 '13 at 08:51
  • @CodeCaster: It doesn't get ignored. The call to `header` does not actually send the status line; it just notes down what status code will be emitted when the status line is actually sent. It does the same with the custom header. You could say that if you pass the status argument then a `header` call is responsible for *two* lines of headers, but I don't like that description because the status line will be sent anyway, even with no `header` calls. – Jon Aug 13 '13 at 08:54
  • So in order to let a PHP script send a custom status-line with the `header()` function you'll also have to send a bogus header? Yeah, then you'll need to extend the language with new functions like `http_response_code()`... – CodeCaster Aug 13 '13 at 08:55
  • 1
    @CodeCaster: Yes, although on my machine the header is not actually sent if it does not have a colon. So `header('foo', true, 500)` sets the status code without sending a bogus header. In any case, if the custom header bothers you (and I see no reason why it should) then you can simply use the other option: `header('HTTP/1.1 500')`. – Jon Aug 13 '13 at 08:58