Home > Article > Backend Development > header location php header Detailed instructions and experience on page 1/2
No matter how many headers the page has, it will execute the last one, but it is conditional, for example:
header('Location:http://www.jb51.net');
header('Location:http://www. g.cn');
header('Location:http://www.baidu.com');
This will jump to Baidu
header('Location:http://www.jb51.net'); echo 'This site';
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');
This will jump to google
It is a detailed instruction on the use of the header function
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. The transmitted content needs to be familiar with the HTTP protocol, which has nothing to do with PHP. The traditional header must contain one of the following three headers and can only appear once.
Location: xxxx:yyyy/zzzz
Content-Type: xxxx/yyyy
Status: nnn xxxxxx
Second, let’s first understand how the HTTP protocol works
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
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. The 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 a request Successfully received
3×× To complete the request, the customer needs to further refine the request
4×× Customer error
5×× Server error
IV. Operation examples:
~~~~~~~~~~~~~
< ;1> Redirect function, the most common
Header("Location: http://www.php.net");
?>
<2> forces users to visit this page every time Get the latest data at the same time, rather than using the cache that exists on the client.
//Tell the browser the expiration time of this page (expressed in Greenwich Mean Time), as long as it is a date that has passed.
header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
//Tell the browser the last updated date of this page (expressed in Greenwich Mean Time), which is the same day, the purpose is to force the browser to obtain Latest information
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");
//File length
header("Content-Length: 227685");
//Accepted range units
header("Accept-Ranges: bytes");
//Missing Time-saving file name in the file saving dialog box
header("Content-Disposition: attachment; filename=$filename");
?>
<3> Output status value to the browser, mainly used for access permission 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 As shown, the browser will display that the page does not exist
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>
Note : The traditional header must contain one of the following three headers and can only appear once. Content-Type: xxxx/yyyy Location: xxxx:yyyy/zzzz Status: nnn xxxxxx can appear more than twice in the new multipart header specification (Multipart MIME).
Usage Example
Example 1: This example redirects the browser to the official website of PHP.
Header("Location: http://www.php.net"); exit; >?
Example 2: If you want the user to get the latest data every time, instead of the data in the Proxy or cache, you can use The following header
header("Expires: Mon, 26 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 3: Let the user's browser display the file not found Information.
header("Status: 404 Not Found"); >?
Example 4: Allow users to download files.
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=filename");
header("Content-Description: PHP3 Generated Data"); >?
Current page 1/2 12Next page
The above introduces header location php header detailed usage instructions and usage experience on page 1/2, including header location content. I hope it will be helpful to friends who are interested in PHP tutorials.