PHP header () Usage tips
The following is a detailed description of the use of the header function:
Regardless of how many headers the page has, it executes the last one, but conditionally, for example:
This will jump to Baidu
The role of the header function
PHP only in the HTTP protocol to the HTML document header sent to the browser, tell the browser how to handle the page, as for the content of the transfer will need to familiarize yourself with the HTTP protocol, not related to PHP.
The traditional header must contain one of the following three headers and can only occur once.
How the HTTP protocol works
The HTTP protocol is based on the request/response paradigm. After a client establishes a connection to the server, it sends a request to the server in the form of a Uniform Resource Identifier, protocol version number, followed by MIME information including the request modifier, client information, and possible content. After the server receives the request, it gives the corresponding response information in the form of a status line that includes the protocol version number of the information, a successful or incorrect code, followed by MIME information including server information, entity information, and possible content.
It is divided into four processes, in the HTTP protocol, the server is the part that provides the HTTP service, the client refers to the browser you use or download tools and so on. In the communication, the client makes a request connection, the server establishes the connection, and then the client makes an HTTP request (request) and the server returns the response information (Respond), thus completing an HTTP operation.
The meaning of the HTTP protocol status Code representation
1XX reserved 2XX indicates request successfully received 3xx for completion request customer needs further refinement request 4XX customer error 5xx Server error
Operation Example
- The redirection feature is the most common.
- Instead of using a client-side cache, the
- forces users to get the latest information each time they access the page.
- Output status values to the browser, primarily for access control.
For example, to restrict a user from accessing the page, you can set the status to 404, as shown below, so the browser shows that the page does not exist.
Note: The traditional header must contain one of the following three headers and can only occur once. CONTENT-TYPE:XXXX/YYYY location:xxxx:yyyy/zzzz status:nnn xxxxxx can appear more than two times in the new multi-header specification (Multipart MIME).
Usage examples
- Example one: This example redirects the browser to the official PHP website.
Header ("location:http://www.php.net/"); Exit
- Example two: to the user each time to get the latest information, instead of Proxy or cache data, you can use the following headers:
Header ("Expires:mon, Jul 1997 05:00:00 GMT"); Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT"); Header ("Cache-control:no-cache, must-revalidate"); Header ("Pragma:no-cache");
- Example three: Let the user's browser appear unable to find the file information.
- Example four: Let the user download the file.