We tend to optimize caching settings on the server, but we seldom notice
client-Side Caching, which is exactly the caching mechanism of the browser.
In fact, each browser has a cache policy, will temporarily cache each of the files viewed in a special folder. We can tell the user that the page has not changed and can call the cache when the user repeatedly submits the page request. So how do we know if the user has cached data for this page? In fact, the browser will send the request when the HTTP header is sent, generally like this:
Date:sun, Jul 2006 09:18:11 GMT
Content-type:image/gif
last-modified:wed, Jul 2006 07:40:06 GMT
ETag: "8c55da8d6abc61:2327"
content-length:14757
which
last-modified:wed, Jul 2006 07:40:06 GMT
ETag: "8c55da8d6abc61:2327"
is the cache information about the page. Then if the response code returned by the server is not HTTP (OK), but 304, the browser will read the data from the cache.
Tells the client browser not to use the cache, HTTP 1.1 protocol
Header ("Cache-control:no-cache, must-revalidate");
Tells the client browser not to use the cache, compatible with the HTTP 1.0 protocol
Header ("Pragma:no-cache");
According to this principle, can be used in infrequently updated or need to refresh the page, can greatly reduce the burden of the server, because if it found that the client has a cache, send a 304 response to the client, and then stop the execution of the program.
The browser makes a request that contains if-modified-since and if-none-match two parameters, the first of which indicates whether the last modification time of the query data is thu,19 June 16:24:01 GMT and the server checks the last modified time of the data. If this is the time, the status Code 304 (indicating no modification) is returned, and when the browser receives a status code of 304, it does not download the data but is called from the local cache. However, only the data for the requested resource exists in the local cache, the browser sends the If-modified-since parameter and its value is the last-returned by the previous server The value of Modified (not all servers support if-modified-since and If-none-match), and If-none-match functions Similarly, it is generated by the value of the ETag returned by the server, can be any value, This is because it only makes it possible for the server to check the modification time of the data and then return it, as long as it is not none (the default) or is not empty.
So we can set the ETag returned to the browse to a value in the first part of the code, and then when the resource is requested by the second time, a if-none-match parameter is attached. by verifying that its value is indeed the ETag value emitted, you can specify that the server returns to 304 and then forcibly quit the program. If-modified-since is the same thing. Only the PHP version of the ETag method is given (the last-modified version is too common, such as setting the cache timeout, etc.):
PHP code copied to clipboard
Copy CodeThe code is as follows:
if ($_server["http_if_none_match"] = = "Claymorephp.com")
{
Header (' Etag: '. ') Zhaiyun.com ', true,304);
Exit ();
}
else {
Header (' Etag: '. ') Claymorephp.com ");
}
You can also change a little bit:
$expires =date ("YMD"); Cache expires after one day
if ($_server["http_if_none_match"] = = $expires)
{
Header (' Etag: '. $expires, true,304);
Exit ();
}
else {
Header (' Etag: '. $expires);
}
if ($_server["http_if_none_match"] = = "Claymorephp.com") {header (' Etag: '. ') Zhaiyun.com ', true,304); Exit (); } else {header (' Etag: '. ') Claymorephp.com "); You can also change it a little bit: $expires =date ("YMD"); A day after the cache expires if ($_server["http_if_none_match"] = = $expires) {header (' Etag: '. $expires, true,304); exit ();} else {header (' E Tag: '. $expires); }
In addition, when Gzip and etag are used at the same time sometimes problems, that is, ETag has no value, the problem is universal, I have not found the relevant reasons, the Internet search for a while, the General people call it a bug.
For these reasons, the client cache for Phpblog is handled as follows (both Http_if_none_match and http_if_modified_since are judged):
PHP code copied to clipboard
Copy CodeThe code is as follows:
if ($_server[' Http_if_none_match ')
{
if ($_server[' http_if_none_match '] = = ' Phpblog ')
{
Header (' Etag:phpblog ', true,304);//Control browser cache
$_session[' Time_end ']=microtime (true);
Exit ();
}
}
else if ($_server[' http_if_modified_since '])//eg:sun, 07:08:25 GMT; length=35849
{
$array =explode (' ', $_server[' http_if_modified_since ');
$gmday = $array [1];
$month _array=array (
"Jan" = "01",
"Feb" = "02",
"Mar" = "03",
"APR" = "04",
"May" = "05",
"June" = "06",
"Jul" = "07",
"The" 08 ",
"Sep" = "09",
"Oct" = "10",
"Nov" = "11",
"Dec" = "12");
$gmmonth = $month _array[$array [2]];
$gmyear = $array [3];
$array =explode (': ', $array [4]);
$gmtimestamp =gmmktime ($array [0], $array [1], $array [2], $gmmonth, $gmday, $gmyear);
if (Gmmktime ()-$gmtimestamp < $config _client_cache_time*60*60)
{
Header (' Etag:phpblog ', true,304);//Control browser cache
$_session[' Time_end ']=microtime (true);
Exit ();
}
}
if ($_server[' Http_if_none_match ')} {if ($_server[' http_if_none_match '] = = ' Phpblog ') {header (' Etag:phpblog ', true, 304);//control browser cache $_session[' Time_end ']=microtime (true); Exit (); }} else if ($_server[' http_if_modified_since '])//eg:sun, at 07:08:25 GMT; length=35849 {$array =explode (', $_server[' http_if_modified_since '); $gmday = $array [1]; $month _array=array ("Jan" = > "he", "Feb" and "he", "Mar" and "he", "APR" and "he", "may" and "he", "June" and "he", "Jul" and "" " "," "Sep" = "", "Oct" and "Ten", "Nov" and "One", "Dec" = "12"; $gmmonth = $month _array[$array [2]]; $gmyear = $array [3]; $array =explode (': ', $array [4]); $gmtimestamp =gmmktime ($array [0], $array [1], $array [2], $gmmonth, $gmday, $gmyear); if (Gmmktime ()-$gmtimestamp < $config _client_cache_time*60*60) {header (' Etag:phpblog ', true,304);//control browser cache $_ session[' Time_end ']=microtime (true); Exit (); } }
The cache header is sent in this way:
PHP code copied to clipboard
Copy CodeThe code is as follows:
$client _cache_time= $config _client_cache_time*60*60;//Units-Sec
Header (' Cache-control:public, max-age= '. $client _cache_time);
Header (' Expires: '. Gmdate (' d, D M Y h:i:s ', time () + $client _cache_time). ' GMT ');//Set Page cache time
Header (' last-modified: '. Gmdate (' d, D M Y h:i:s ', Time ()). ' GMT ');//Return Last modified Time
Header (' Pragma:public ');
Header (' Etag:phpblog ');//return identity, used to identify the last time it was accessed (cache exists in the browser)
$client _cache_time= $config _client_cache_time*60*60;//Unit-Second header (' Cache-control:public, max-age= '. $client _cache_ Time); Header (' Expires: '. Gmdate (' d, D M Y h:i:s ', time () + $client _cache_time). ' GMT ');//Set the page cache time header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT ');//returns the last modified time header (' pragma:public '); Header (' Etag:phpblog ');//return identity, used to identify the last time it was accessed (cache exists in the browser)
http://www.bkjia.com/PHPjc/327397.html www.bkjia.com true http://www.bkjia.com/PHPjc/327397.html techarticle we tend to optimize the cache settings on the server, but we seldom notice the client cache, which is exactly the caching mechanism of the browser. In fact, every kind of browser ...