HTTP Header Fields are components of the header section in the HTTP request* and HTTP response* messages, transmitted from client to server. The last two might be a web browser and a web server. HTTP header fields contain instructions which determine the HTTP data transfer from server to client. The general HTTP Headers (request, response) are defined in the HTTP protocol.
Full list of all permanent headers as well as free to download provisional headers, added by developers, is available at IANA: Message Headers.
HTTP header fields consist of field name and field value. They can be generally divided into two types: request fields (used by the client) or response fields (used by the server). However, some HTTP headers such as Cache-Control, Via, Content-Length, Content-Type, etc. might be simultaneously used by client and server.
How to View HTTP Headers?
HTTP request or response headers are viewed via web browser tools such as Web Console for Firefox that can be initiated with the keyboard shortcut Ctrl + Shift + K.
You can also get HTTP headers by using the following PHP functions: getallheaders() apache_request_headers() for request or apache_response_headers() for response.
Here are some common client request headers:
|request line||GET /http-headers-request-response-header-fields.html HTTP/1.1|
|HTTP headers (request)||Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8|
|Accept-Encoding: gzip, deflate|
|User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Firefox/45.0|
Here are some common server response headers:
|status line||HTTP/1.x 200 OK|
|HTTP headers (response)||Content-Encoding: gzip|
|Content-Type: text/html; charset=utf-8|
|Last-Modified: Wed, 30 Mar 2016 08:15:07 GMT|
Managing HTTP Headers
HTTP headers are managed and configured in the mod_headers Apache module through modifying the .htaccess file. For example:
<IfModule mod_headers.c> Header append Vary: User-Agent </IfModule>
HTTP headers can be also managed through the applications of the hosting account. This can be done via PHP, for example to set up the Last-Modified header that indicates the last modification date of the document:
$modify_time = filemtime($file); header("Last-Modified: " . gmdate("D, d M Y H:i:s", $modify_time) . " GMT");
You can set HTTP server response headers through the header() PHP function. The headers_list() provides information which HTTP headers have been or will be sent to client. The headers_sent() enables you to check if and where the HTTP headers have been sent.