Php browser cache _ PHP Tutorial

Source: Internet
Author: User
Php browser cache. The browser cache is totally different. the cached content is stored locally in the browser, but the content is generated by the web server. neither party can complete this system process independently, this is totally different. the cached content is stored locally in the browser, but the content is generated by the web server. neither party can complete this system process independently, therefore, there must be a communication mechanism between them, which is "cache negotiation" in http ".
Let's first look at a common http request:
Host www.bkjia.com
User-agent mozilla/5.0 (windows; u; windows nt 6.1; zh-cn; rv: 1.9.2.9) gecko/20100824 firefox/3.6.9 firephp tutorial/0.4
Accept text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8
Accept-language zh-cn, zh; q = 0.5
Accept-encoding gzip, deflate
Accept-charset gb2312, UTF-8; q = 0.7, *; q = 0.7
Keep-alive 115
Connection keep-alive
The above is a common http request. in order to implement browser cache control, we need to understand the four tags in the http response header:
Last-modified: last modified gmt time
Etag: indicates whether the content has been changed by encoding.
Expires: specifies an expiration gmt time.
Cache-control: mag-age = the number of expired seconds relative to the local browser (this can prevent the server from taking effect if the server time is incorrect)

Status Code returned by http:
200 -- request successful
304 -- the client has executed get, but the file has not changed
400 -- Incorrect request, such as syntax error
500 -- Internal error occurred on the server
501 -- the server does not support the requested function

The following code uses the above four methods to control the browser content cache for one hour. if the browser returns a 304 status code within one hour, it notifies the browser to use the local content. This will save the server program execution time and network transmission time (only one header file is returned)
$ Modified_time = $ _ server ['http _ if_modified_since '];
If (strtotime ($ modified_time) + 3600> time ()){
Header ("http/1.1 304 ");
Exit (0 );
}
Header ("last-modified:". gmdate ("d, d m y h: I: s"). "gmt ");
Header ("expires:". gmdate ("d, d m y h: I: s", time () + 3600). "gmt ");
Header ("cache-control: max-age = 3600 ");

Note that expires requires the server provider to support the expires module, which is not enabled by default. use the following command:
A2enmod expires // enable the expires module
/Etc/init. d/apache2 restart // restart the apache service
You can also modify the http. conf configuration:
Expiresactive on expiresbytype image/gif "access plus 1 month" expiresbytype text/css tutorial "now plus 2 day" expiresdefault "now plus 1 day"
The above sets the expiration time for the mime type of static content, because we cannot directly specify an absolute expiration time for static files, so we use the "access plus" syntax, when the content is requested, the web server dynamically calculates an absolute expiration time as the content marked as expires.
It is worth mentioning that for common static file formats, even if the web server returns an http response header without the expires mark, the browser will also guess an expiration time based on some other clues, for example, in some cache mode, if the gifimage is set to never expire, the expiration time is set to the current time or 0 unless the expires is set to expire immediately.


Success ,...

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.