Header location PHP header detailed instructions and tips for use 1th/2 page

Source: Internet
Author: User
Tags php website
Regardless of how many headers the page has, it executes the last one, but conditionally, 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 station ';
Header (' location:http://www.g.cn ');
Header (' location:http://www.baidu.com ');
This will jump to Google
The following is a detailed description of the use of the header function
First, the role:
~~~~~~~~~
PHP only in the HTTP protocol to the HTML document header to the browser, tell the browser how to handle this 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.
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. 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.
Third, the meaning of the HTTP protocol status Code representation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1XX reserved
2XX indicates that the request was successfully received by
3XX request further refinement of requests for completion of customer requests
4XX Customer Error
5XX Server Error
Four, the operation example:
~~~~~~~~~~~~~
<1> redirection features, the most common
Header ("Location:http://www.php.net");
?>
<2> forces users to get the latest information each time they access the page, rather than using a cache that exists on the client.
Tell the browser how long this page will expire (in Greenwich Mean Time), as long as the date has passed.
Header ("Expires:mon, Jul 1970 05:00:00 GMT");
Tells the browser that the last update date of this page (in Greenwich Mean Time) 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");
Tell the client browser not to use the cache
Header ("Cache-control:no-cache, must-revalidate");
Parameters (compatible with previous servers), which is compatible with the 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 the File Save dialog box by default
Header ("content-disposition:attachment; Filename= $filename ");
?>
<3> output status value to the browser, mainly for access rights control
Header (' http/1.1 401 Unauthorized ');
Header (' status:401 unauthorized ');
?>
For example, to restrict a user from accessing the page, you can set the status to 404, as shown below, so that the browser is displayed as if 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 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: Users can get the latest information each time, rather than 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.
Header ("status:404 not Found"); ?
Example four: Let the user download the file.
Header ("Content-type:application/x-gzip");
Header ("content-disposition:attachment; Filename= file name ");
Header ("Content-description:php3 Generated Data"); ?

Current 1/2 Page 12 next page

The above describes the header location PHP header detailed instructions and use of the 1th page 2, including the header location aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.