PHP and browser cache

Source: Internet
Author: User
Tags http 200 response code
We often optimize cache settings on the server, but we seldom pay attention to the client cache, accurate to 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 send the IF-modified-Since parameter and its value is the last-returned result of the server only when data from the requested resource exists in the local cache-
Modified values (not all servers support if-modified-since and if-None-match); If-None-match functions are similar, it is generated by the etag value returned by the server and can be any value, because it only serves to make the Server check the data modification time and then return it back, as long as it is not none (default) or not empty.


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 is too common, such as setting cache timeout .)
  • 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 also change it to: $ expires = date ("ymd"); // 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 determined): the PHP code is copied to the clipboard.
  • 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: phpblog', true, 304); // controls browser cache
$ _ Session ['time _ end'] = microtime (true); exit () ;}} the cached header is sent as follows: copy the PHP code to the clipboard.

  • $ 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.