In-depth analysis of PHP and browser cache _ php instances

Source: Internet
Author: User
Tags http 200
This article provides a detailed analysis of PHP and browser cache. For more information, see the various optimization solutions on the server. Client CacheTo be precise, it is the browser cache mechanism.
In fact, each browser has a cache policy, which temporarily caches each browsed file in a special folder. When you submit a page request repeatedly, you can call the cache to tell the user that the page has not changed. So how do we know if the user has cached data on this page? In fact, when sending a request, the browser will first send an http header, generally like this:
Date: Sun, 30 Jul 2006 09:18:11 GMT
Content-Type: image/gif
Last-Modified: Wed, 19 Jul 2006 07:40:06 GMT
ETag: "8c55da8d6abc61: 2327"
Content-Length: 14757
Where
Last-Modified: Wed, 19 Jul 2006 07:40:06 GMT
ETag: "8c55da8d6abc61: 2327"
Is about the page cache information. Then, if the response code returned by the server is not HTTP 200 (OK), but 304, the browser will read data from the cache.
// Tell 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 and is compatible with the HTTP 1.0 Protocol
Header ("Pragma: no-cache ");
Based on this principle, it can be used on pages that are not updated frequently or need to be refreshed frequently, which can greatly reduce the burden on the server, because if it finds that the client has a cache, it will send a 304 response to the client, then stop the program execution.

The request sent by the Browser contains the If-Modified-Since and If-None-Match parameters. The first parameter indicates whether the last modification time of the data is Thu, 19 Jun 2008 16:24:01 GMT then the server will check the last modification time of the data. If this time is used, status code 304 is returned (indicating that no modification is made ), in this case, when the browser receives a status code of 304, it does not download data but calls it from the local storage. However, the browser will only send the If-Modified-Since parameter If the requested resource data exists in the local cache and its value is the Last-Modified value returned by the server (not all servers support If-Modified-Since and If-None-Match ); the If-None-Match function is similar. It is generated by the Etag value returned by the server and can be any value, because it only enables the server to check the data modification time and return it back, as long as it is not none (default value) or not empty, it is OK.

Therefore, we can set the Etag returned to the browser as a value in the first part of the code, then, when the resource is requested for the second time, an If-None-Match parameter is attached, when verifying that the value is indeed the Etag value, you can specify that the server returns 304 and then forcibly exit the program, if-Modified-Since is the same. In this case, only the php version of The etag method is provided. (Last-Modified version is too common, such as setting cache timeout ):
Copy PHP code to clipboard

The 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 it a bit:
$ Expires = date ("Ymd"); // The 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 change it to $ expires = date ("Ymd "); // cache expiration after one day if ($ _ SERVER ["HTTP_IF_NONE_MATCH"] ==$ expires) {header ('etag :'. $ expires, true, 304); exit () ;}else {header ('etag :'. $ expires );}


In addition, when GZIP and ETAG are used at the same time, problems may occur, that is, there is no value for ETAG. This problem is common and I have not found any relevant reasons for the moment. I searched the internet for a while, most people call it a BUG.
Based on the above reasons, the client cache for PHPBLOG is processed (both HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE are judged ):
Copy PHP code to clipboard

The Code is as follows:


If ($ _ SERVER ['HTTP _ IF_NONE_MATCH '])
{
If ($ _ SERVER ['HTTP _ IF_NONE_MATCH '] = 'phpblog ')
{
Header ('etag: phpblog', true, 304); // controls browser cache
$ _ SESSION ['time _ end'] = microtime (true );
Exit ();
}
}
Else if ($ _ SERVER ['HTTP _ IF_MODIFIED_SINCE ']) // eg: Sun, 02 Nov 2008 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 ",
"Jun" => "06 ",
"Jul" => "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, $ gmyear );
If (gmmktime ()-$ gmtimestamp <$ config_client_cache_time * 60*60)
{
Header ('etag: phpblog', true, 304); // controls 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 the browser cache $ _ SESSION ['time _ end'] = microtime (true); exit ();}} else if ($ _ SERVER ['HTTP _ IF_MODIFIED_SINCE ']) // eg: Sun, 02 Nov 2008 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 ", "Jun" => "06", "Jul" => "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, $ gmyear); if (gmmktime () -$ gmtimestamp <$ config_client_cache_time * 60*60) {header ('etag: phplogg', true, 304 ); // control the browser cache $ _ SESSION ['time _ end'] = microtime (true); exit ();}}


The cached HEADER is sent as follows:
Copy PHP code to clipboard

The Code is as follows:


$ 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'); // sets the page cache time.
Header ('Last-Modified: '. gmdate ('d, d m y h: I: s', time (). 'gmt'); // return the Last modification time
Header ('pragma: public ');
Header ('etag: phpblog'); // return ID, used to identify the last visit (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'); // sets the page cache time header ('Last-Modified :'. gmdate ('d, d m y h: I: s', time ()). 'gmt'); // return the last modification time header ('pragma: public'); header ('etag: phpblog '); // return ID, used to identify the last visit (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.