In-depth analysis of PHP and browser cache _php instance

Source: Internet
Author: User
Tags explode php code response code browser cache
We tend to do a variety of optimizations on the server for caching settings, but we rarely notice Client Cache, which is exactly the caching mechanism of the browser.
In fact, each browser has a caching policy, will temporarily cache each of the scanned files in a special folder. We can call the cache when the user repeats the page request and tells the user that the page has not changed. So how do we know that the user has no cached data for this page? In fact, the browser sends the request to send the HTTP header first, generally like this:
Date:sun, June 2006 09:18:11 GMT
Content-type:image/gif
Last-modified:wed, June 2006 07:40:06 GMT
ETag: "8c55da8d6abc61:2327"
content-length:14757
which
last-modified:wed, June 2006 07:40:06 GMT
ETag: "8c55da8d6abc61:2327"
Is the cached information about the page. Then, if the response code returned by the server is not HTTP (OK), but 304, the browser reads the data from the cache.
Tells the client browser not to use caching, HTTP 1.1 protocol
Header ("Cache-control:no-cache, must-revalidate");

Tells the client that the browser does not use caching, which is 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 on 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 sends a request containing the if-modified-since and if-none-match two parameters, the first indicates whether the last modification time of the data is thu,19 June 2008 16:24:01 GMT then the server checks the last modification time of the data. If this is the time, return status code 304 (for no modification), and when the browser receives a status code of 304, it will not download the data but call from the local cache. However, the browser sends the If-modified-since parameter only if there is data for the requested resource in the local cache, and its value is the last-returned by the previous server Modified values (not all servers support if-modified-since and if-none-match); If-none-match functions are similar, it is generated by the value of ETag returned by the server, can be any value, 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 value) or not empty.

So we can return a value to the browse ETag in the first part of the code, and then a if-none-match argument comes in when the resource is requested a second time. You can specify that the server returns to 304 and then forcibly quit the program by verifying that its value is indeed a etag value emitted, and if-modified-since is the same way. Just give the PHP version of the ETag method (the last-modified version is too common, such as setting the cache timeout, and so on):
PHP code copied to clipboard
Copy Code code 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 a little bit: $expires =date ("YMD"); One day after cache expiration 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 no value, this problem is universal, I temporarily did not find the relevant reasons, online search for a while, the common people called bugs.
For the above 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 Code code 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, Nov 2008 07:08:25 GMT; length=35849
{
$array =explode (', $_server[' http_if_modified_since '));
$gmday = $array [1];
$month _array=array (
"=>", "01",
"Feb" => "02",
"Mar" => "03",
"APR" => "04",
"May" => "05",
"June" => "06",
"June" => "07",
"Aug" => "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, and $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, Nov 2008 07:08:25 GMT; length=35849 {$array =explode (', $_server[' http_if_modified_since ']) $gmday = $array [1]; $month _array=array > "", "Feb" => "," "=>" "," "APR" => "," "may" => "," June "=>" "," "" => "" "," "" "," "," "Aug" => " "", "Sep" => "," "Oct" => "ten", "Nov" => "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, and $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 cached header is sent in this way:
PHP code copied to clipboard
Copy Code code as follows:

$client _cache_time= $config _client_cache_time*60*60;//Unit-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 ');//Returns an identity to identify the last true access (cache exists in the browser)
$client _cache_time= $config _client_cache_time*60*60;//Unit-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 header (' last-modified: '. Gmdate (' d, D M Y h:i:s ', Time ()). ' GMT '); Returns the header (' Pragma:public ') of the last modified time; Header (' Etag:phpblog ');//Returns an identity to identify the last true access (cache exists in the browser)

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.