Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header order question in PHP (and HTTP in general)

Is there a particular order in PHP to set HTTP headers with the header() function ?

I mean, for example, must I call

header('Content-Language: en');

before

header('Content-Type: text/plain');

or does the order not matter?

My guess is that order isn't important as long as all the headers are set before any content is outputted, but I just want to be sure that's the case...

like image 769
AlexV Avatar asked Jan 19 '10 14:01

AlexV


People also ask

What are general headers in HTTP?

General header is an outdated term used to refer to an HTTP header that can be used in both request and response messages, but which doesn't apply to the content itself (a header that applied to the content was called an entity header).

What is HTTP header in PHP?

The header in PHP is a PHP built-in function for sending a raw HTTP header. The HTTP functions are those that manipulate information sent by the webserver to the client or browser before it sends any further output. The header() function in PHP sends a raw HTTP header to a client or browser.

How can I get HTTP header in PHP?

Receiving the request header, the web server will send an HTTP response header back to the client. Read any request header: It can be achieved by using getallheaders() function. Example 2: It can be achieved by using apache_request_headers() function.

Does the order of headers matter HTTP?

The order in which header fields with differing field names are received is not significant. However, it is "good practice" to send general-header fields first, followed by request-header or response- header fields, and ending with the entity-header fields.


2 Answers

No, the order of the header fields doesn’t matter:

The order in which header fields with differing field names are received is not significant. However, it is "good practice" to send general-header fields first, followed by request-header or response- header fields, and ending with the entity-header fields.

Only if you’re sending multiple fields of the same name. Then the field values are treated like they would appear in one list:

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

So the following:

Cache-Control: private
Cache-Control: must-revalidate

would be equivalent to:

Cache-Control: private, must-revalidate

And here it depends on the definition of the header field (here Cache-Control) if the order does matter.

like image 141
Gumbo Avatar answered Sep 25 '22 08:09

Gumbo


The order in which header fields with differing field names are received is not significant. However, it is "good practice" to send general-header fields first, followed by request-header or response- header fields, and ending with the entity-header fields.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 (Hypertext Transfer Protocol -- HTTP/1.1)

like image 20
Thomas Müller Avatar answered Sep 24 '22 08:09

Thomas Müller