SoFunction
Updated on 2025-04-04

Header function usage instructions

1. Function:  
~~~~~~~~~  
PHP only uses the HTTP protocol to send the header of the HTML document to the browser, telling the browser how to deal with this page. As for the content transmitted, you need to be familiar with the HTTP protocol, which has nothing to do with PHP. You can refer to http:///Protocols/rfc2616/rfc2616.
Traditional headers must contain one of the following three headers and can only appear once.
       Location:  xxxx:yyyy/zzzz  
       Content-Type:  xxxx/yyyy  
       Status:  nnn  xxxxxx  
 
2. Let’s first understand how the HTTP protocol works
  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
The HTTP protocol is based on the request/response paradigm. After a client establishes a connection with the server, a request is sent to the server. The format of the request method is: a unified resource identifier, protocol version number, followed by MIME information including request modifier, client information and possible content. After receiving the request, the server gives corresponding response information, which is in the format of a status line including the protocol version number of the information, a successful or error code, followed by the 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 part that provides HTTP services, and the client refers to the browser or download tool you use, etc. During communication, the client issues a request to connect, and the server establishes a connection; then, the client issues an HTTP request (Request), and the server returns a response information (Respond), thereby completing an HTTP operation.
 
3. The meaning of HTTP protocol status code
  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
1××Reserve
2×× means the request was successfully received
3×× to complete the request, the customer needs to further refine the request.
4××Customer Error
5×× server error

4. Operation examples:  
~~~~~~~~~~~~~  
<1>  The redirect function, this is the most common
       <?php  
           Header("Location:  ");    
       ?>  

<2>  Forcing users to get the latest information every time they visit this page, rather than using caches that exist on the client.
       <?php  
//Tell the browser the expiration date of this page (indicated by Greenwich time), as long as it is an elapsed date.
           header("Expires:  Mon,  26  Jul  1970  05:00:00  GMT");  
//Tell the browser the last update date of this page (indicated by Greenwich time) that is the day, the purpose is 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 cache
           header("Cache-Control:  no-cache,  must-revalidate");  
// Parameters (compatible with previous servers), 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");      
//Accepted range unit
           header("Accept-Ranges:  bytes");  
//By default file name in the file saving dialog box
           header("Content-Disposition:  attachment;  filename=$filename");  
       ?>  

<3>  Output status value to the browser, mainly used for access permission control
       <?php  
           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 will display that the page does not exist.
       <?php  
           header('HTTP/1.1  404  Not  Found');  
           header("status:  404  Not  Found");  
       ?>