Home > Article > Backend Development > Instructions for using header() function_PHP tutorial
1. Function:
~~~~~~~~~
PHP just sends the header of the HTML document to the browser using the HTTP protocol, telling the browser how to process this page. As for the transmitted content, you need to be familiar with the HTTP protocol. It has nothing to do with PHP. You can refer to http://www.w3.org/Protocols/rfc2616/rfc2616.
Traditional headers must contain one of the following three headers and can only appear once.
Location: xxxx: YYYY/ZZZZ
Content-Type: xxxx/YYYY
Status: nnn xxxxxx
Two, first understand the operation method of the http protocol
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTTP protocol is based on the request/response paradigm. After a client establishes a connection with the server, it sends a request to the server. The format of the request is a uniform resource identifier, a protocol version number, followed by MIME information including request modifiers, client information and possible content. After receiving the request, the server gives corresponding response information. Its format is a status line including the protocol version number of the information, a success or error 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 refers to the part that provides HTTP services, and the client refers to the browser or download tool you use, etc. During communication, the client sends a request for connection, and the server establishes the connection; then, the client sends an HTTP request (Request), and the server returns response information (Respond), thus completing an HTTP operation.
3. The meaning of HTTP protocol status code
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
1×× Reserved
2×× Indicates that the request was successfully received
3×× In order to complete the request, the customer needs to further refine the request
4×× Customer error
5×× Server Error
4. Operation examples:
~~~~~~~~~~~~~
<1> Redirect function, this is the most common
Header("Location: http://www.php.net");
?>
<2> Force users to visit this page every time Get the latest data instead of using the client-side cache.
Header ("Expires: Mon, 26 Jul 1970 05:00:00 gmt");
// Tell the browser's final update date (represented by Greenwich time), which is the same day, the purpose is, the purpose is Force the browser to obtain the latest data
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
//Tell the client browser not to Use cache
header("Cache-Control: no-cache, must-revalidate");
//Parameters (compatible with previous servers), that is, compatible with HTTP1.0 protocol
header("Pragma: no-cache");
//Output MIME type
header("Content-type: application/file"); Content-Length: 227685") ; . -Disposition: attachment; filename=$filename");
?>
<3> Output the status value to the browser, mainly used for access control
header( 'HTTP/1.1 401 Unauthorized');
header('status: 401 Unauthorized');
?>
For example, if you want to restrict a user from accessing this page, you can set the status to 404, as follows display, so that the browser displays that the page does not exist
🎜> ?>
http://www.bkjia.com/PHPjc/317079.html
www.bkjia.com
true