The header() function is very used in PHP, and I will introduce some methods of using it to implement page caching, but before using header, it must be noted that no output can be done before it, including spaces.
Settings that are not cached
In the manual, we all write about how to set cache so that the code is not cached:
And when setting up, you must also pay attention to the fact that there can be no output before the header, otherwise the header setting will be invalid.
Page or image cache settings
How to set up a cache for a page, although we know there are ways to do it, such as E-TAG. Of course, there are also simple settings: For example, we do MD5 on the content before output, and treat it as an e-tag as long as it doesn't change, it won't have any impact. (This is similar to server status code 304, where the requested page has not been modified since the last request.) When the server returns this response, the web content is not returned. ) There are other ways too:
Cache for 1 hour, mainly the expiration time has to be set with gmdate, not date, this should be noted, the others are similar. max-age should be compatible with expire.
For dynamic content generated by PHP, you only need to output a header that forces the use of caching before the content is output, for example, the following code requires the browser to cache the file for 1 month:
|