PHP header () function usage Instructions _php tips

Source: Internet
Author: User
Tags http request
Header () Function usage Description:

first, the role:
~~~~~~~~~
PHP is only the HTTP protocol to the HTML document to the browser, tell the browser how to deal with this page, as for the transfer of the content you need to familiarize yourself with the HTTP protocol, and PHP has nothing to do, can refer to http://www.w3.org/Protocols/rfc2616/rfc2616.
The traditional header must contain one of the following three kinds of headers and can only appear once.
Location:xxxx:yyyy/zzzz
Content-type:xxxx/yyyy
status:nnn xxxxxx

second, first to understand 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. When the server receives a request, it responds with a response that is formatted as a status line that includes the protocol version number of the information, a successful or erroneous 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 provision of HTTP services, the client refers to the browser you use or download tools and so on. When communicating, the client issues a request connection, the server establishes the connection, then the client sends an HTTP request (request), and the server returns the response information (Respond), which completes an HTTP operation.

third, the HTTP protocol status code means
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1xx Retention
2XX indicates that the request was successfully received
3xx requesting customers to further refine requests for completion
4XX Customer Error
5XX Server Error

Iv. Operation Example:
~~~~~~~~~~~~~
<1> redirect function, this most common
Copy Code code as follows:

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


<2> force users to get the most up-to-date information each time they visit this page, instead of using a client-side cache.
Copy Code code as follows:

<?php
Tells the browser when this page expires (in Greenwich Mean Time), as long as the date is already past.
Header ("Expires:mon, June June 1970 05:00:00 GMT");
Tell the browser that the last update date for this page (in GMT) is the same 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");
Tells the client that the browser does not use the cache
Header ("Cache-control:no-cache, must-revalidate");
Parameter (compatible with previous server), i.e. compatible 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");
File name in File Save dialog box by default
Header ("content-disposition:attachment; Filename= $filename ");
?>


<3> output status value to browser, mainly for access rights control
Copy Code code as follows:

<?php
Header (' http/1.1 401 Unauthorized ');
Header (' status:401 unauthorized ');
?>

For example, to limit a user's inability to access the page, you can set the status to 404, as shown below, so that the browser appears as if the page does not exist
Copy Code code as follows:

<?php
Header (' http/1.1 404 Not Found ');
Header ("status:404 not Found");
?>

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.