HEADER () function instructions

xiaoxiao2021-03-06  82

HEADER () function instructions:

First, the role:

~~~~~~~~~

PHP is only sent to the HTTP protocol header to the browser, telling the browser how to handle this page, as for the content of the transfer, you need to be familiar with the HTTP protocol, not related to PHP, can refer to http: // www. w3.org/protocols/rfc2616/rfc2616.

The traditional header must contain one of the three headers below and can only appear once.

Location: XXXX: YYYY / ZZZZ

Content-Type: XXXX / YYYY

Status: nnn xxxxxx

Second, let's take a look at the way HTTP protocol works.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The HTTP protocol is based on request / response. After a client is connected to the server, send a request to the server, the request method, the unified resource identifier, the protocol version number, and the MIME information includes request modifiers, client information, and possible content. After the server is connected, the corresponding response information is given, the format is a state line including the protocol version number, a successful or error code, and the MIME information includes server information, entity information, and possible content.

It is divided into four processes. In the HTTP protocol, the server refers to the part of the HTTP service, and the client refers to the browser or download tool you use. When communicating, a request connection is issued by the client, and the server is established; then, the client issues an HTTP request (Request), the server returns the response information (respond), thereby completing an HTTP operation.

Third, the meaning of the HTTP protocol status code

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 × × reserved

2 × × indicates that the request is successfully received

3 ×× To complete the request customer needs further refinement request

4 × × customer error

5 ×× server error

Fourth, the operation example:

~~~~~~~~~~~~~

<1> Redirect function, this most common

Header ("Location: http://www.php.net");

?>

<2> Forced users to get the latest information each time you visit this page, not the cache where the client is used.

// Tell the browser's expiration time (represented by Greenwich time) as long as it has passed the date.

Header ("Expires: MON, 26 JUL 1970 05:00:00 GMT");

// Tell the browser that the last update date of this page (represented by Greenwich time) is the day, the purpose is to force the browser to get the latest information

Header ("Last-Modified:". gmdate ("D, D M Y H: I: S"). "GMT");

// Tell the client browser without cache

Header ("Cache-Control: no-cache, must-revALIDATE");

// Parameter (compatible with the previous server), is compatible with HTTP1.0 protocol

Header ("prgma: no-cache";

/ / Output MIME type

Header ("Content-Type: Application / File);

// File length

Header ("Content-Length: 227685);

// Accepted range unit

"Accept-Ranges: Bytes");

/ / The file name in the file save dialog box is default

Header ("Content-Disposition: attachment; filename = $ filename");?>

<3> Output status value to the browser, mainly for access control

HEADER ('http / 1.1 401 unauthorized');

HEADER ('Status: 401 Unauthorized');

?>

For example, if you restrict a user who cannot access the page, you can set the status 404, as shown below, this browser is displayed as that page does not exist

HEADER ('http / 1.1 404 not found ");

Header ("Status: 404 Not Found");

?>

转载请注明原文地址:https://www.9cbs.com/read-88861.html

New Post(0)