Syntax:
Parameter Description
string Required. Specifies the header string to be sent.
replace is optional. Indicates whether this header replaces the previous header, or adds a second header.
Default is true (replacement). false (allow multiple headers of the same type).
http_response_code is optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and above)
Common usage examples:
1. Sending server status code
Status Code Description
100-199 indicates that the request has been successfully received, and the client is required to continue submitting the next request to complete the entire processing process.
200-299 indicates that the request has been successfully received and the entire processing process has been completed. 200 is commonly used
300-399 The request is not completed. The client needs to further refine the request. For example, the requested resource has been moved to a new address. Commonly used are 302,304
400-499. The client request is incorrect. Commonly used are 404
500- 599 An error occurred on the server side, commonly used 500
For details, please see: HTTP&FTP corresponding prompt information
2. Page jump
Force the browser not to cache!
[code]
//header('Expires:-1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache -Control:no-cache,must-revalidate');
header('pragma:no-cache');
header('Last-Modified: '. date('D, j M Y H:i:s T') );
Press Enter in the address bar
If the value is private or Must-revalidate, the server will only be accessed the first time, and will not be accessed in the future. If the value is no-cache, it will be accessed every time. If the value is max-age, it will not be accessed again before expiration.
Press the back button
If the value is private, must-revalidate, max-age, it will not be accessed again, and if it is no-cache, it will be accessed again every time
Press the refresh button
Regardless of the value, it will be accessed repeatedly. When the Cache-control value is specified as "no-cache", accessing this page will not leave a page backup in the Internet temporary article folder. In addition, caching can also be affected by specifying the "Expires" value. For example, if you specify the Expires value as a time that has long passed, then if you press Enter repeatedly in the address bar when accessing this website, the access will be repeated every time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
About "Last-Modified
When the browser requests a URL for the first time, the return status from the server will be 200, the content is the resource you requested, and there is a Last-Modified attribute to mark this file in the service The time when the end of the period was last modified, the format is similar to this:
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the provisions of the HTTP protocol, the browser The server will send an If-Modified-Since header to the server, asking whether the file has been modified after this time:
If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT
If the server-side resource has not changed, it will automatically return HTTP 304 (Not Changed.) status code with empty content, thus saving the amount of data transferred. When the server-side code changes or the server is restarted, the resource is reissued and the return is similar to the first request. This ensures that resources are not sent to the client repeatedly, and also ensures that when the server changes, the client can get the latest resources.
Last-Modified improves performance
Smart developers will use Last-Modified together with the http header of the ETags request, which can take advantage of the cache of the client (such as the browser). Because the server generates the Last-Modified/Etag tag first, the server can use it later to determine whether the page has been modified. Essentially, the client is asking the server to validate its (client-side) cache by passing this token back to the server.
The process is as follows:
The client requests a page (A)
The server returns page A and adds a Last-Modified to A
The client displays the page and caches the page together with Last-Modified
The client requests page A again and passes the Last-Modified returned by the server in the last request to the server
The server checks the Last-Modified and determines that the page has not been modified since the last client request, and directly Returns a 304 response and an empty response body.
3. File download
For example, download a pdf file