PHP header () function use detailed

Source: Internet
Author: User
Tags session id php website response code

Note: No matter how many headers the page has, it executes the last one, but is conditional, for example:

    1. Header (' location:http://bbs.it-home.org ');
    2. Header (' location:http://www.g.cn ');
    3. Header (' location:http://www.baidu.com ');
Copy Code

This will jump to Baidu

    1. Header (' location:http://bbs.it-home.org ');
    2. Header (' location:http://www.g.cn ');
    3. Header (' location:http://www.baidu.com ');
Copy Code

This will jump to Google

Detailed instructions for using 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, 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, 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 HTTP protocol status code 1XX reserved 2XX indicates that the request successfully received 3xx to complete the request customer needs further refinement request 4XX customer error 5xx Server error

iv. Examples of Operation (1), redirection function, this most common

 
      

(2), forcing users to access this page each time to get the latest information, rather than using the presence of the client's cache.

 
      

(3), output status value to the browser, mainly for access rights control

 
      

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:

 
      

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). Example one using example: This example redirects the browser to the official PHP website.

    1. Header ("location:http://www.php.net/"); Exit
Copy Code

Example two: Users can get the latest information each time, rather than Proxy or cache data, you can use the following headers

    1. Header ("Expires:mon, Jul 1997 05:00:00 GMT");
    2. Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT"); Header ("Cache-control:no-cache, must-revalidate");
    3. Header ("Pragma:no-cache");
Copy Code

Example three: Let the user's browser appear unable to find the file information.

    1. Header ("status:404 not Found");
Copy Code

Example four: Let the user download the file.

    1. Header ("Content-type:application/x-gzip");
    2. Header ("content-disposition:attachment; Filename= file name ");
    3. Header ("Content-description:php3 Generated Data");
Copy Code

Header-Sends an original HTTP header stating the void header (string string [, bool replace [, int http_response_code]]) the header () function is used to send an original HTTP headers. See the http/1.1 specification for more information about HTTP headers. The optional parameter, replace, indicates whether to replace the previous similar header or to add a header of the same type. The default is replace, but if you set it to FALSE, you can force multiple, homogeneous headers to be sent. For example:

 
      

The Second optional parameter, Http_response_code, forces the HTTP response code to be set to the specified value (this parameter is a new addition to PHP 4.3.0). There are two special types of header calls. The first is that the header begins with the string "http/" (not important in case) and can be used to determine the HTTP status code to send. For example, if Apache is configured with PHP to handle error-handling requests that cannot find files (using the ErrorDocument Directive), you need to make sure that the script produces the correct status code.

 
      

Note: The HTTP status code header line is always the first to be sent to the client, and does not control whether the actual header () call is the first one.  Unless the HTTP header has been sent, you can overwrite the original by invoking the header () function with the new state line at any time. The second special case is the "location:" header. It does not just send the label back to the browser, it also returns a REDIRECT (302) status code to the browser, unless a 3XX status code has been issued before.

 
      

Note: The http/1.1 standard requires an absolute address URI as the location: parameter, but there are some clients that support relative URIs. You can usually use $_server[' http_host ', $_server[' php_self '] and the dirname () function to generate an absolute URI from a relative URI:

 
      

Note: Even if the session.use_trans_sid,session ID is enabled, it will not be passed along with the location header information.    Must be passed manually with SID constants. PHP scripts often produce dynamic content that must not be cached by the browser or proxy server. Many proxy servers and browsers can be disabled by the following methods:

 
      

Note: You may find that even if you do not output all of the above code, the page is not buffered. There are many options that users can set to change the browser's default caching behavior.    By sending the above header, you should be able to overwrite any settings that could cause the script page to be cached.    In addition, when a session is used, the Session_cache_limiter () function and the Session.cache_limiter option can be used to automatically generate the correct cache-related headers. Remember that the header () must be called before any actual output, whether it's from a normal HTML tag, a blank line, or PHP. One common mistake is that when reading code through include (), require (), or some other file access class function, some spaces or blank lines are sent before the header () is called. Also in a separate php/html file, this error is common.

 
      

Note: Since PHP 4, some output buffering functions can be used to solve this problem. The price is to keep all the output from the browser on the server until the next command sends them. You can use Ob_start () and Ob_end_flush () in your code to implement such functionality, either by modifying the output_buffering configuration options in php.ini, or by modifying the server configuration file.

Header () Two common uses:

    1. To set the page encoding:
    2. Header (' content-type:text/html;charset=gb2312 ');
    3. Adjustment page:
    4. Header (' location:http://bbs.it-home.org ');
Copy Code

you may be interested in the article:A simple example of a PHP header () function PHP header function Implementation file download instance code PHP header function Usage Examples of PHP headers using the detailed PHP header function File download direct prompt to save the code PHP header information Application example PHP file header information detailed PHP header message Details PHP header function Use points

  • Related Article

    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.