header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // Date in the past
代码如下 |
复制代码 |
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts"); header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
|
And when setting, you have to pay attention that there can be no output before the header, otherwise the header setting will be invalid, but I have never written about how to set the Cache for the page, although we know that there are some methods, such as E-TAG and the like. Of course there are also simple settings:
代码如下 |
复制代码 |
header("Cache-Control: public");
header("Pragma: cache");
$offset = 30*60*60*24; // cache 1 month
$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT";
header($ExpStr);
?>
|
For example, before outputting, we perform md5 on the content and treat it as an e-tag. As long as there is no change, there will be no impact. There are other ways:
The code is as follows
代码如下 |
复制代码 |
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType text/css A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/html A600
|
|
Copy code
|
$seconds_to_cache = 3600; |
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts"); header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
Cache for 1 hour. The main reason is that the expiration time must be set with gmdate instead of date. You should pay attention to this. Everything else is similar. maxage must be consistent with expire.
For dynamic content generated by PHP, you only need to output the forced caching header before the content is output. For example, the following code requires the browser to cache the file for 1 month:
The code is as follows
|
Copy code
|
header("Cache-Control: public");<🎜>
header("Pragma: cache");<🎜>
<🎜> $offset = 30*60*60*24; // cache 1 month<🎜>
$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT";<🎜>
header($ExpStr);<🎜>
?>
For static files, general servers support level 3 cache status. If you want to achieve the fourth-level caching effect, you can either use PHP to outsource a layer like the previous GZIP compression, and then use PHP to process it. Either you need server-side support, mod_expires, a module of APACHE, supports adding expires header to files. Add the following code to the .htaccess file in your blog directory. If your server has the mod_expires module installed, it will automatically take effect. Images, etc. are forced to be cached for one month, and HTML documents are cached for 10 minutes. If the module is not installed, no error occurs.
The code is as follows
|
Copy code
|
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType text/css A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/html A600
There is more detailed documentation and tutorials for mod_expires here. But what I want to point out is that mod_expires is not installed on most servers
http://www.bkjia.com/PHPjc/631539.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631539.htmlTechArticleThe header() function is widely used in PHP. Let me introduce some methods of using it to achieve page caching. But before using the header, you must note that no output, including spaces, can be done before it. ...
|
|
|