PHPheader () function usage _ PHP Tutorial

Source: Internet
Author: User
Use of the PHPheader () function. In the previous article, I introduced some situations of the header () function in PHP. I would like to add it here. The following is a detailed description of the header function: no matter how many headers are on the page, in the previous article, I introduced some situations of the header () function in PHP. I would like to add it here.

The following describes how to use the header function:

No matter how many headers there are on the page, it executes the last one, but it is conditional. for example:

header('Location:http://www.bkjia.com'); header('Location:http://www.g.cn'); header('Location:http://www.baidu.com'); 

This will jump to Baidu

Role of the header function

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.

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

HTTP status code
1 × 2 × indicates that the request has been successfully received. 3 × to fulfill the request, the customer needs to further refine the request. 4 × customer error. 5 × server error.
Operate instances
  • Redirection function, which is the most common.
  •    
  • Force users to obtain the latest information each time they access this page, instead of using the cache that exists on the client.
  •    
  • Outputs the status value to the browser, which is mainly used for access permission control.
  •    

    For example, if you want to restrict a user from accessing this page, you can set the status to 404, as shown below, so that the browser shows that this page does not exist.

       

    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 MIME Type.

Example
  • Example 1: In this example, the browser is redirected to the official PHP website.
  • Header("Location: http://www.php.net/"); exit;  
  • Example 2: to obtain the latest data every time, instead of the data in the Proxy or cache, you can use the following headers:
  • 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: make the user's browser unable to find the file information.
  • header("Status: 404 Not Found"); 
  • Example 4: allow users to download files.
  • Header ("Content-type: application/x-gzip"); header ("Content-Disposition: attachment; filename = file name"); header ("Content-Description: PHP3 Generated Data ");

Some situations of the header () function in PHP are described here. The following describes how to use the header function: no matter how many headers exist on the page ,...

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.