Phpheader () function setting Page Cache _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
The phpheader () function sets the page Cache. The header () function is widely used in php. I will introduce some methods to implement page cache using it. However, before using the header, you must note that no output can be made before it, contains spaces. The header () function is widely used in php. I will introduce some methods to implement page cache using it. However, before using the header, you must note that no output can be made before it, contains spaces.

In the manual, we write how to set 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


In addition, you must note that no output is available before the header. Otherwise, the header settings are invalid but have not been written. how to set Cache for the page, although we know there are some ways, such as E-TAG. Of course, there are also simple settings:

For example, if we perform md5 on the content before the output and treat it as an e-tag, it will not be affected if it does not change. There are other methods:

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 ");


Cache for 1 hour, mainly because gmdate is used to set the expiration time, instead of date. Note that the rest is similar. Maxage must be able to match expire.

For dynamic content generated by PHP, you only need to output the mandatory cache header before the content is output. for example, the following code requires the browser to cache the file for one month:

The code is as follows:

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, the server generally supports a cache status of 3rd. To achieve the fourth level of cache effect, you can use PHP to outsource a layer and then use PHP to process the layer as before GZIP compression. Either the server-side support is required. the mod_expires module of APACHE supports adding the expires header to the file. Add the following code to your blog directory. htaccess file. if the mod_expires module is installed on your server, the file takes effect automatically. images are forcibly cached for one month, and html files are cached for 10 minutes. If this module is not installed, no error will occur.

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


Here are more detailed documents and tutorials for mod_expires. However, mod_expires is not installed on most servers.

The http://www.bkjia.com/PHPjc/631539.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631539.htmlTechArticleheader () function is widely used in php. below I will introduce some methods to use it to implement page cache, but before using the header, you must note that there is no output before it, including spaces ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.