PHP Control Client Cache

Source: Internet
Author: User
Tags http authentication

1. About HTTP Messages

See: http://www.runoob.com/http/http-messages.html
The request header in an HTTP message is the information the browser wants to tell the server, and the response header is the information the server wants to tell the client. The definitions of these headers are defined by the HTTP protocol, and the client and server complete their work by parsing the information in the message header. For example: The accept-language:zh-cn,zh;q=0.8 in the request header tells the server that the client can receive the language.

2. Response Header Cache-control Control cache


Cache-control a generic header for defining cache directives.

Cache Policy:
Useful Cache-control response header information includes:

  max-age=[seconds-the maximum time to execute the cache is considered to be up to date. Similar to the expiration time, this parameter is based on the relative time interval of the request time, not the absolute expiration time, [seconds] is a number in seconds: the number of seconds from the start of the request to the expiration time. s-maxage=[sec-Similar to the Max-age property, except that he applies the cache to the shared (such as proxy server) public-tag authentication content can also be cached, in general: content that is accessed by HTTP authentication, output is automatically not cacheable Private in the case of the server setting private such as: Cache-control:private, max-age=60, indicates that only the user's browser can cache the private response and not allow any relay Web proxy to cache it – for example, The user browser can cache HTML pages that contain user private information, but the CDN cannot be cached. No-cache-forces each request to be sent directly to the source server without verifying the local cached version. This is useful for confirming the authentication application (which can be used in conjunction with public) or for applications that use the latest data (all the benefits of using the cache at the expense of caching); no-store-does not cache must-revalidate-tells the cache to follow all the freshness you give to the copy. The cache must be sent every time the request to the server checksum, such as through If-modified-since or if-none-matchproxy-revalidate-and must-revalidate similar, except that he only to the cache proxy server function, Example: cache-control:max-age=3600, Must-revalidate  
 <?phpheader ("Content-type:text/html;charset=utf-8"); //Use Cache-control to tell the browser to be valid for 5 seconds ("cache-control:max-age=5"); //equivalent to Cache-control:public, max-age=5 $curr _time = date (' y-m-d h:i:s '); Echo ' server time: '. $curr _time; ?> <! DOCTYPE html><html><head><meta charset="Utf-8"><title>Cache</title></head><body><br>Time: <?phpecho $curr _time; ?>   <br>    <a href=' cache_sample.php '>Refresh Time</a></body></html>

3. Response header Expires control cache
<?php  header ( " Content-type:text/html;charset=utf-8 "); //using Cache-control to tell the browser is valid for 5 seconds  //header ("cache-control:max-age=5");//equivalent to Cache-control:public, max-age=5  //set the validity period of 10 seconds, the time format is GMT  //time is the current server time + 10 seconds   $expires  = gmdate (, Time () + 10 ).  ' GMT ' ; Header ( "Expires: $expires" );  $curr _time  = date ( ' y-m-d h:i:s ' ); echo   ' server time: ' .  $curr _time ; ?>  <! DOCTYPE html><html><head><meta charset="Utf-8"><title>Cache</title></head><body><br>Time: <?phpecho $curr _time; ?>   <br>    <a href=' cache_sample.php '>Refresh Time</a></body></html>


Expires the disadvantage is that the validity period is set by the server time, if the client time and service side time has a certain difference, the cache control has a problem, so it is recommended to use Cache-control good!

If the above code is set at the same time

header("Cache-Control:max-age=5");$expires= gmdate(‘D, d M Y H:i:s‘+10.‘ GMT‘//这个设置将无效header("Expires:$expires");

Cache-control (support) and expires also set the validity period, whichever is the Cache-control setting

4. Using the cache via HTTP 304:not Modified

(1) The response header contains Expires, max-age words, "Open new Window", "Address bar input URL Enter" These browser behavior will not allow the browser in Expires, Max-age set the validity period to access the server, but in the cache to get content, but "refresh" or "F5" exception.
(2) Access the server and obtain the content according to the server response. This happens when the Cache-control:no-cache is set to whether the cache must be authenticated by the server, or if Expires,max-age is set but the browser behavior is "refresh" or "F5". ' Last-modified ', ' ETag ', ' must-revalidate ' and other special, not directly affected by browser behavior, they must access the server, then by the server to determine whether to send a new resource directly, or send a 304 not Modfied lets the browser use the resources in the cache.

Note: Ctrl + F5 is a forced flush.


The ETag is the full entity Tag, used to identify a resource. In a specific implementation, the ETag can be a hash value for a resource, or it can be an internally maintained version number. However, the etag should be able to reflect changes in resource content, which is the basis for HTTP caching to work properly. The last-modified principle is similar to the ETag, which last-modified through time to identify resources. In this way, you do not have to get all the resources every time to achieve the purpose of updating, can greatly save the cost of the server, more conducive to search engine crawl.

<?phpHeader"Content-type:text/html;charset=utf-8"); Header ("Cache-control:no-cache");//Response head last-modified$lastmodified= Filemtime ('./cache.php ');$lastmodifiedGMT= Gmdate (' d, D M Y h:i:s ',$lastmodified).' GMT '; Header ("last-modified: $lastmodifiedGMT");//Response header ETag$etag= Md5_file ('./cache.php '); Header ("ETag: $etag");if(@$_server[' Http_if_modified_since '] ==$lastmodifiedGMT|| @trim ($_server[' Http_if_none_match ']) ==$etag) {Header ("http/1.1 304 not Modified");Exit; }//Return 304, the following content will not be returned to the browser, and the browser will use the previous cache$curr _time= Date (' y-m-d h:i:s ');Echo ' server time: '.$curr _time;?><! DOCTYPE html><html><head><meta charset="Utf-8"><title>Cache</title></head><body><br>Time: <?phpecho $curr _time; ?>   <br>    <a href=' cache_sample.php '>Refresh Time</a></body></html>

Reference:
[1] Introduction to the HTTP caching principle for back-end programmers
[2] Web cache Acceleration Guide for webmasters and site administrators [translate]
[3] Caching cache details

PHP Control Client Cache

Related Article

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.