You get a bonus - 1 coin for daily activity. Now you have 1 coin

HTTP headers

Lecture



HTTP headers are used to "communicate" with the browser and the web server, for example, when the browser requests a document, it sends a GET header, and when the server returns a document type, it does not do this, but in the Content-type header .

We have already partially reviewed the HTTP headers, but now we’ll focus on them in more detail.

So here’s a list and brief description of the main HTTP headers.

Accept Header

The Accept header is intended to inform the server about the data types that are supported by the client (browser). In this header, the browser lists which types of documents it “understands”. Pere-
numbering is comma separated.

The HTTP_ACCEPT environment variable is used. Example of use:

Accept: text/html, text/plain, image/jpeg

Recently, instead of a list, the value is *. *, Which means "all types".

Content-type header

This header is intended to identify the type of data transmitted. At the same time, the Content-type header uses the CONTENT_TYPE environment variable. Typically, this header is set to application / x-www-form-urlencoded. Thus, a format is indicated in which all control characters (i.e., non-alphanumeric characters) are specifically encoded. You can find out about some other MIME types here.

This is the same transfer format used by the GET and POST methods.

Another format, multipart / form-data, is quite common.

The server does not interpret the header in question, but simply passes it to the script via the environment variable.

Example: Content-type: text/plain

Content-length header

This header contains a string containing the length of the transmitted data in bytes when using the POST transfer method. For the title is fixed variable CONTENT_LENGTH.

If the GET method is enabled, then this header is missing, which means that the environment variable is not set.

Cookie Header

All cookies are stored in this header. This header uses the HTTP_COOKIE environment variable. To set cookies, use the Set-Cookie header.

Header get

We mentioned this heading earlier.

The GET header uses the following environment variables:

  • REQUEST_URI - the requested resource identifier;
  • QUERY_STRING - parameters passed to the script;
  • REQUEST_METHOD - information transfer method. In this case, this variable will contain the value GET.

Location Header

After receiving the Location header along with the URL specified in it, the server immediately goes to the specified URL, without waiting for the document body to load:

Example: Location: http://www.somehost.com/

POST Header

This header uses the same environment variables as the GET header (the REQUEST_METHOD variable contains the value POST). Recall that data using the POST method can be transmitted at the end of headers.

Recall the format of the POST header: POST сценарий?параметры HTTP/1.0

Pragma Header

This header is used for various purposes, one of which is to prohibit caching of the document.

Header example: Pragma: no-cache

Header Server

This header contains the name and version of the server software. For example:

Server: Apache/1.3.23 (Unix) (Red-Hat/Linux) mod_ssl/2.8.7 OpenSSL/0.9.6b Dav/1.0.3 PHP/4.3.0 mod_perl/1.26 configured

Referer Header

With this header you can find out the server that refers to us. This is useful when we need to identify backlinks, for example, when analyzing the effectiveness of network advertising.

Environment variable: HTTP_REFERER.

User-Agent Header

Contains browser version. For example: User-Agent: Mozilla / 5.0 (compatible; Konqueror / 3.0.0-10; Linux).

Environment variable: HTTP_REFERER.

Some comments on HTTP headers

We have familiarized with titles of headings and environment variables corresponding to them.

It is necessary to remember the basic principles:

  • All characters are uppercase;
  • HTTP_ is added at the beginning of the names
  • The "-" characters are replaced by the underscore "_".

Passing HTTP Headers to PHP

PHP has built-in functions for working with HTTP headers.

The header () function is used to transfer HTTP headers.

Let's give practical examples:

header(" Content-type: text/plain ");
?>

header("Location: http://www.example.com/"); /* Производит перенаправление браузера на другой ресурс */

/* Внимание! Убедитесь, что код, расположенный ниже не исполняется */
exit;
?>

// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");
?>

See also:

»HTTP / 1.1 specification

»HTTP protocol features

created: 2016-01-25
updated: 2021-03-13
132407



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)