How to set cache time in php

藏色散人
Release: 2023-03-05 14:40:01
Original
3696 people have browsed it

In PHP, you can use the "header()" method to set the page Cache cache. The syntax is such as "header("Pragma: cache");$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s", ),GMT";".

How to set cache time in php

Recommended: "PHP Video Tutorial"

php header() sets the page Cache cache

The header() function is widely used in php. Below I will introduce some methods of using it to achieve page caching, but you must before using the header Note that there cannot be any output before it, including spaces.

In the manual, we all write about how to set up the cache so that the code is not cached. The code is as follows:

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
Copy after login

And in When setting, you must also pay attention to the fact that there cannot be 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, of course there are also simple settings. ,For example, before outputting, we md5 the content and treat it as an e-tag. As long as it does not change, there will be no impact. There are other ways, the code is as follows:

$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");
Copy after login

Cache for 1 hour, mainly for expiration The time must be set with gmdate, not date. This should be noted. Everything else is similar. maxage must match expire.

For dynamic content generated by PHP, you only need to output forced caching before the content is output. The header is enough. For example, the following code requires the browser to cache the file for 1 month. The code is as follows:

<?php 
  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); 
?>
Copy after login

For static files, general servers support the third level cache state. To reach the fourth level For the caching effect, either use PHP to outsource a layer like the previous GZIP compression, and then use PHP to process it. Or you need server-side support. Mod_expires, a module of APACHE, supports adding expires header to files. Add the following code to your blog directory .htaccess file, 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, there will be no error. The code is as follows:

<IfModule mod_expires.c> 
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 
</IfModule>
Copy after login

Here are more detailed documentation and tutorials for mod_expires. However, what I want to point out is that mod_expires is not installed on most servers.

The above is the detailed content of How to set cache time in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!