This article is a mirror article of machine translation, please click here to jump to the original article.

View: 6809|Reply: 0

php header() function to set the page cache

[Copy link]
Posted on 7/26/2019 1:59:17 PM | | |
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:







Previous:Responsible leadership
Next:Java oss Alibaba Cloud upload file settings download file name
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com