Header () function instructions _ PHP Tutorial

Source: Internet
Author: User
Header () function usage instructions. I. Role :~~~~~~~~~ PHP only sends the HTML document header to the browser using the HTTP protocol to tell the browser how to handle the page. As for the transmitted content, you need to familiarize yourself with the HT I. role:
~~~~~~~~~
PHP only sends the HTML document header to the browser using the HTTP protocol to tell the browser how to handle the page. As for the transmitted content, you need to familiarize yourself with the HTTP protocol, which is irrelevant to PHP, see http://www.w3.org/protocols/rfc2616/rfc2616.
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

II. First, let's take a look at how HTTP works.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTTP is based on the request/response paradigm. After a client establishes a connection with the server, it sends a request to the server in the format of uniform resource identifier and protocol version number, the MIME Information is followed by the request modifier, client information, and possible content. After receiving the request, the server sends a response in the format of a protocol version number containing the information, a successful or wrong code in the status line, MIME Information is followed by server information, Entity Information, and possible content.
It consists of four processes. in the HTTP protocol, the server refers to the part that provides the HTTP service, and the client refers to the browser or download tool you use. During communication, the client sends a Request to connect and the server establishes a connection. then, the client sends an HTTP Request, and the server returns the response information (Respond) to complete an HTTP operation.

III. meaning of HTTP status code

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 × reserved
2×× indicates that the request is successfully received.
3×× further refine the request to complete the request
4×× customer error
5×× server error

4. operation example:
~~~~~~~~~~~~~
<1> redirection function, which is the most common
Header ("Location: http://www.php.net ");
?>

<2> force users to obtain the latest information each time they access this page, instead of using the cache that exists on the client.
// Tell the browser the expiration time of this page (expressed by 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 update date (expressed by Greenwich Mean Time) of this page, that is, the day, to force the browser to obtain the latest information
Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
// Tell the client browser not to use the cache
Header ("Cache-Control: no-cache, must-revalidate ");
// Parameter (compatible with the previous Server), 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 ");
// Unit of acceptable range
Header ("Accept-Ranges: bytes ");
// The file name in the "Save saved files" dialog box
Header ("Content-Disposition: attachment; filename = $ filename ");
?>

<3> outputs the status value to the browser, which is mainly used for access permission control.
Header ('http/1.1 401 unauthorized ');
Header ('status: 401 unauthorized ');
?>
For example, to restrict a user from accessing this page, you can set the status to 404, as shown below, so that the browser shows that the page does not exist.
Header ('http/1.1 404 Not Found ');
Header ("status: 404 Not Found ");
?>

:~~~~~~~~~ PHP only sends the HTML document header to the browser using the HTTP protocol and tells the browser how to handle the page. As for the transmitted content, you need to familiarize yourself with HT...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.