Use PHP to disable the cache problem of IE and Firefox_PHP tutorial

WBOY
Release: 2016-07-21 15:14:25
Original
740 people have browsed it

After searching for many ways to improve the network speed, I finally solved it
In fact, the simplest way is to add the tag in the header



You can also use program control

Copy the code The code is as follows:

header("Cache-control:no-cache,no-store,must-revalidate");
header("Pragma:no-cache");
header("Expires:0");
?>

If in or header("Cache-control:no-cache,no-store, must- revalidate"); without no-store, Firefox's cache cannot be solved
The following is a specific analysis for you:
Two caches of Firefox and IE browsers Important differences
After you create a WEB service, there are usually two types of cache that need to be configured:
Set the html resource to expire immediately when the website is updated, so that users who are browsing can quickly Get updated.
Set all other resources (such as images, CSS, javascript scripts) to expire after a certain period of time.
This caching solution covers some of the ideas mentioned in the Two Simple Rules for HTTP Caching article on how to handle updates.
Now that HttpWatch 6.0 supports Firefox, we would like to discuss how Firefox handles cache differently from IE. The method of setting a longer expiration time (the second item above) can still be used directly in Firefox, but configuration 1 is There are still subtle differences between the two.
In the previous article, we divided the first one into:
Sometimes dynamic HTML pages need to be updated from the server immediately to be displayed at any time - even using back button. For example, displaying the status of a bank account or an online order.
Static HTML pages, such as contact, FAQs or sitemaps, if they set the Last-Modified response header, allow the browser to reload when needed Verification, you can take advantage of the cache.
The rest of this article discusses two important differences that affect HTML page caching in Firefox.

1. Use no-cache to prevent Firefox cache invalidation
You can simply set the following response header to prevent IE from caching anything:
Cache-Control: no-cache
Pages using this response header will not be saved in the cache, and IE will always will be reloaded from the server; even if you use the back button. The following example uses HttpWatch to monitor an online store. When we click the back button after submitting the order form, the result is as follows:

However, this response The header cannot prevent Firefox from caching. This means that under normal access conditions, Firefox will always use the cached page until it sends a GET request to recheck. And if the page is accessed through the back button, Firefox will not access it again. server, but simply loads directly from the cache.

So how can you turn off the cache in Firefox? The answer is simple, it cannot be turned off. Because Firefox relies on the copy in the cache as "File-> Save As ", "View Source" operations. However, you can control where the page is cached and which cache entries can be used for display.
The following response headers prevent persistent caching in Firefox, forcing the page to be cached into memory:
Cache-Control:no-store
This header can also prevent the cached page from being accessed when using the back button, which will trigger an HTTP GET request.
The combination of the values ​​​​of these two response headers You can get the expected results in IE and Firefox by using:
Cache-Control: no-cache, no-store
As shown in the following HttpWatch response header tag:
no-store and no-cache headers

2. If you do not set an expiration time, Firefox will set one for you. When IE encounters an http response without an Expires header, it thinks that the cache entry can never be automatically used until it is re-validated from the service. Due to IE's A setting item of the temporary file "Check for a newer version of the web page" defaults to "Auto", so it is usually done once per session.
This provides a reasonable way to control the caching of static HTML content. The user's newly opened IE will get the latest version of the html, and the cached version will be used until I close IE.
Firefox handles missing Expires headers differently. If there is a Last-Modified header in the impact, it will use it A tentative expiration value specified in the HTTP 1.1 specification RFC2616:
(quoting specification :)
And, if there is a Last-Modified time value in the response, the tentative expiration value cannot exceed a ratio of the time interval from this value to the present. Generally, this ratio is set to 10%.
The calculation method is as follows:
Expiration time = current time + 0.1 * (time difference from Last-Modified to now)
For example, if your static HTML file was last modified 100 days ago, the expiration time is 10 days later. The following example It is an HttpWatch cache tag without an Expires header page:
pic3
Firefox automatically sets the expiration time to 8 days later, because this page has not been modified for about 80 days.
This means that in order to maintain control For your HTML pages, as we discussed in the Two Simple Rules for HTTP Caching article, you'd better set an appropriate Expires value on your WEB server for your static resources such as HTML, images, CSS files, etc.

Conclusion
To ensure consistent caching behavior between IE and Firefox, you should:
Always specify an Expires header. Generally set -1 to use html pages to refresh instantly or Set a specific expiration time for other resources such as images, CSS, and javascript
If you want to force the page to refresh, even when clicking the background button, then set Cache-Control: no-cache, no-store

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326259.htmlTechArticleI found a lot of ways to improve the network speed, but finally solved it. In fact, the simplest way is to add meta tags in the header. META HTTP-EQUIV="Cache-Control" CONTENT="no-cache,no-store, must-revalidate" ME...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!