In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "PHP and browser how to achieve caching", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "PHP and browser how to achieve caching" this article.
We often optimize the cache settings on the server, but we pay little attention to the client cache, or rather the browser cache mechanism.
In fact, every browser has a caching strategy, which temporarily caches every browsed file in a special folder. When the user repeatedly submits the page request, we can tell the user that the page has not changed, and we can call the cache. So how do we know if users have cached data for this page? In fact, browsers send http headers first when sending requests, usually 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
Among them
Last-Modified: Wed, 19 Jul 2006 07:40:06 GMT
ETag: "8c55da8d6abc61:2327"
It is about the cached information of the page. Then if the response code returned by the server is not HTTP 200 (OK), but 304, the browser will read the data from the cache.
/ / tell client browsers not to use caching, HTTP 1.1 protocol
Header ("Cache-Control: no-cache, must-revalidate")
/ / tell client browsers not to use caching and compatible with HTTP 1.0protocol
Header ("Pragma: no-cache")
According to this principle, it can be used in 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 sends a 304 response to the client, and then stops the execution of the program.
The request sent by the browser contains two parameters, If-Modified-Since and If-None-Match. The first means to ask whether the last modification time of the data is Thu,19 Jun 2008 16:24:01 GMT, and then the server will check the last modification time of the data. If it is this time, it will return the status code 304( indicating no modification). At this time, when the browser receives the status code is 304, it will not download the data but will call it from the local cache. However, the browser will send the If-Modified-Since parameter only if the data for the requested resource exists in the local cache and its value is the value of the Last- Modified returned by the last server (not all servers support If-Modified-Since and If-None-Match) The function of If-None-Match is similar, it is generated by the value of Etag returned by the server, which can be any value, because its function is only to make the server check the modification time of the data and return it, as long as it is not none (the default) or anything else.
So we can set the Etag returned to browsing to a certain value in the first part of the code, and then when the resource is requested for the second time, it will be accompanied by an If-None-Match parameter. By verifying that its value is indeed the emitted Etag value, we can specify that the server returns to 304 and forcibly exit the program. The same is true for If-Modified-Since. Only the php version of the etag method is given here (the Last-Modified version is too common, such as setting cache timeout, etc.):
Copy the PHP code to the clipboard
The copy 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 little bit:
$expires=date ("Ymd"); / / the cache expires one day later
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 slightly: $expires=date ("Ymd"); / / cache expired if after a day ($_ SERVER ["HTTP_IF_NONE_MATCH"] = $expires) {header ('Etag:'.$expires,true,304) Exit ();} else {header ('Etag:'.$expires);}
In addition, there is sometimes a problem when GZIP and ETAG are used at the same time, that is, there is no value for ETAG. This problem is common. I haven't found the relevant reason yet. I searched the Internet for a while, and most people call it BUG.
For the above reasons, the client-side cache for PHPBLOG is handled below (both HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE are judged):
Copy the PHP code to the clipboard
The copy code is as follows:
If ($_ SERVER ['HTTP_IF_NONE_MATCH'])
{
If ($_ SERVER ['HTTP_IF_NONE_MATCH'] = =' phpblog')
{
Header ('Etag:phpblog',true,304); / / controls browser caching
$_ 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 "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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.