Php and browser cache mechanism introduction _ PHP Tutorial

Source: Internet
Author: User
Php and browser cache mechanism introduction. The browser's cache policy temporarily caches browsed files on a local disk. When you repeatedly request a page, you can call the cache to inform the client that the page has not changed. The browser's cache policy will temporarily cache browsed files on the local disk. When you repeatedly request a page, you can call the cache to inform the client that the page has not changed. So how do I know whether the client has a page cache? At the HTTP protocol level, the browser sends the following requests first:

HTTP header:

Connection Keep-Alive
Date Sun, 06 May 2012 18:00:36 GMT
Last-Modified Sun, 06 May 2012 17:31:02 GMT
Etag ec1f629013925ab0fa4389ba926e8c06
Keep-Alive timeout = 15, max = 299
Server Apache/2.2.16 (Unix) DAV/2
Vary Accept-Encoding

Note that these two lines describe the page cache information:

Last-Modified Sun, 06 May 2012 17:31:02 GMT
Etag ec1f629013925ab0fa4389ba926e8c06 in this case, if the server responds to the 304 status code, the browser will consciously read data from the cache; if the server responds to the 200 status code, no matter whether there is a client cache, it will still read.

According to this theoretical support, for example, most of the query results of the webmaster Corps are obtained asynchronously using ajax, and the second access can be modified using this method. As long as the client has a cache, it will send a 304 response status code to the client and then exit program execution.

The request sent by the browser contains the If-Modified-Since and If-None-Match parameters:

If-Modified-Since indicates whether the last modification time of the data is a certain time value. The server then checks the last modification time of the data. if this time is used, the server returns the 304 status code. after receiving the status code, the client directly reads the cache from the local machine. In this case, the browser sends the If-Modified-Since parameter only when there is a cache resource locally, and the value is the Last-Modified value returned by the server.

If-None-Match is similar. it is generated by the Etag value returned by the server and is only used by the server to check the data modification time. it can be any value. Considering that the If-Modified-Since method combined with Last-Modified is not supported by all servers, the etag implementation is only considered here.

In PHP, you can use $ _ SERVER ['http _ IF_NONE_MATCH '] to determine whether the file is cached by the browser. the code snippet is as follows:

// Use the etag tag to control the cache

The code is as follows:
$ Etag = md5 (date ('ymmd '));
If ($ _ SERVER ['http _ IF_NONE_MATCH '] ==$ etag ){
Header ('etag: '. $ Etag, true, 304 );
Exit ();
} Else {
Header ('etag: '. $ Etag );

} Here I use the date of the current day to generate etag, which can ensure that the cache takes effect for up to one day. this parameter can be modified as needed.

Note:
Even if the response is 304, the server will still be requested because a connection needs to be established to determine whether data needs to be transmitted. the 304 cache saves the overhead of static resource transmission;
Another type of cache is the 200 response time cache. If no connection is established, the request will respond to the 200 status code and directly read the cache from the local device.


Bytes. When you repeatedly request a page, you can call the cache to inform the client that the page has not changed. That...

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.