Analysis and application of PHP using HTTP caching protocol in Apache Environment _php Foundation

Source: Internet
Author: User
Tags current time php and php code
For static pages there are etag.

First, first look at the case: Apache static page

The static page that Apache sends to the client typically contains last-modified and etag, the values of which are from the modified time of the static file and the Inode.

The following is the intercept of Apache returned to the client's head

xml/html Code
Copy Code code as follows:

Last-modified:fri, 2007 01:53:34 GMT
ETag: "3f9f640-318-cb9f8380"

Search engines like static files because of these two identities, you can determine whether the file has been updated

Second, PHP and other dynamic pages

Because PHP is dynamically generated, its content is not based on the time of the PHP program to determine the last modified date, so the default PHP return to the client when the patch contains any cache control, to make good use of caching must understand the caching mechanism, and the rationale to reduce the b,s of the interaction, reducing bandwidth flow, reduce server burden ... The benefits abound.

third, the specific meaning of cache control

Explain the meaning of these labels that I have tested and understood first.

Cache-control: Specifies the caching mechanism that the request and response follow. Setting Cache-control in a request message or in a response message does not modify the caching process during another message handling process. The cached instructions at request include No-cache, No-store, Max-age, Max-stale, Min-fresh, only-if-cached, and the instructions in the response message include public, private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, Max-age.

The instructions in each message have the following meanings:

Public indicates that the response can be cached by any buffer.

Private indicates that the entire or partial response message for a single user cannot be handled by the shared cache. This allows the server to simply describe a partial response message from the user, which is not valid for other users ' requests.

No-cache indicates that a request or response message cannot be cached

No-store is used to prevent important information from being inadvertently released. Sending in a request message will not use caching for both request and response messages.

Max-age indicates that the client can receive a response that is not longer than the specified time in seconds.

Min-fresh indicates that the client can receive response times that are less than the current time plus a specified time.

Max-stale indicates that the client can receive response messages that exceed the timeout period. If you specify a value for the Max-stale message, the client can receive a response message that exceeds the specified value for the timeout period.

PHP Usage:

Use header () before output, (if you use Ob_start () you can place headers anywhere in the program)

PHP code
Copy Code code as follows:

Header (' cache-control:max-age=8 ');

Max-age=8 represents the maximum lifetime of 8 seconds, and more than 8 seconds the browser must go to the server to reread, which starts with the user's read page, and expires is the absolute time.

Expires: The absolute time of the cache expiration, if the browser has passed its specified point of time, it will not recognize the cache, to the server to request a new one.

Last-modified: The last modification time of the document, its magical uses is: 1

If it is a static file, the client will send up the time in its cache, Apache will come to compare, if found not modified directly return a head, the status code is 304, the number of bytes is very small, (advanced version will also increase the comparison etag to determine whether the file changes)

2 php Dynamic file:

The client sends on the match time, PHP will judge whether modifies, if the modification time is same, will return only 1024 bytes, as to why returns 1024 is unknown, if your PHP produces the file to be very big, it also only returns 1024, therefore compares the province bandwidth, The client automatically displays from the cached file according to the modified time that the server sends over.

Note: If there are no last-modified headers, Cache-control and expires can also work, but each request returns the actual number of bytes of the file instead of the 1024

How about the four?

Static page does not have to take care of it, if you want to better control the static page of the cache, Apache has several modules can be very good control, here does not discuss

PHP Page:

Here are two types:

1, do not often change the page, similar to the news release, the characteristics of such pages: the first release will have several changes, with the passage of time will not be modified. Control strategy should be: 1 The first release of the Send Last-modified,max-age set 1 days, modified after the update Last-modified,max-age time with the correct number of normal. This seems to be more cumbersome, but also to record the number of changes, you can also predict the next possible modification time with expires specified to approximate time expired

PHP code
Copy Code code as follows:

Header (' cache-control:max-age=86400 ');//Cache Day
Header (' Expires:mon, 2007 08:56:01 GMT ');//Specify Expiration time
Header (' last-modified: ' Gmdate (' d, D M Y 01:01:01 ', $time). ' GMT ')/GMT, $time is the time stamp when the file is added

2 pages that change frequently

Similar to BBS, forum procedures, this page update faster, the main role of caching is to prevent users from frequent refresh list, resulting in server database burden, both to ensure the timeliness of updates, but also to ensure that the cache can be used

Here general use Cache-control to control, according to the forum post frequency flexible control max-age.

PHP code
Copy Code code as follows:

Header (' cache-control:max-age=60 ');/cache One minute
Header (' last-modified: ' Gmdate (' d, D M Y 01:01:01 ', $time). ' GMT ')/GMT, $time is the last update timestamp of the Post

Five extra

1 refresh, go to, force refresh the difference

The browser has refresh and go to the button, and some browsers support the use of Ctrl+f5 Force Refresh page, what is the difference?

Go to: The user clicks the link is to go, it completely uses the caching mechanism, if has the last-modified then does not communicate with the server, uses the grasping package tool to be possible to see the sending byte is 0byte, if the cache expires, then it will perform F5 refresh the action.

Refresh (F5): This refresh is also based on whether the cache has last-modified to decide, if there will be transferred to 304 or 1024 (PHP), if not the last update time then go to the server read, return the real document size

Force refresh: Completely discard caching mechanism, go to the server to read the latest document, send the header to the server as follows

xml/html Code
Copy Code code as follows:

Cache-control:no-cache

2 Debugging Tools

A better tool for viewing browser and server interaction is HttpWatch Pro, now version 4.1, which supports IE7

There are other agent grab kits that can be analyzed by HTTP debugging. No use, there are TCP grab kits, 2000 network. There are also TCP grab kits, 2000 Network Monitor but not specifically for HTTP is more difficult to use.

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.