Home  >  Article  >  Backend Development  >  How to set browser not to cache in php

How to set browser not to cache in php

藏色散人
藏色散人Original
2020-07-10 09:53:054174browse

How to set up php without caching: 1. Set it through the meta tag; 2. Use the statement "Response.CacheControl="no-cache"" to set up no caching; 3. Use the statement "header('Pragma :no-cache'); "to set the php page not to be cached.

How to set browser not to cache in php

How to force the browser not to cache in PHP

Generally we want to view a web page on the Internet, then when you When you visit this webpage for the first time, the system will first download this webpage to a temporary folder on your local computer for caching. When you visit this webpage for the second and third time within a certain period of time, At that time, the browser will directly read the file display from the temporary folder of your local computer. The advantage of this is to avoid re-downloading every time, which takes up a lot of time and network resources. It must be said that the caching of the page is accelerated. Of course, it also has its disadvantages if the web page is not displayed. For example, when the web page on the server has been updated, but the locally displayed web page is still an unupdated web page, the updated content cannot be displayed in a timely manner. In some cases, we need to Disable browser caching so that the pages we see every time are the latest pages (such as background operations, real-time news pages of large websites). At this time, we have to disable browser caching. The following describes the four different Disable web page caching in the page environment:

1. For static pages (set through meta tags):

<!--expires用于设定网页的过期时间,一旦过期就必须从服务器上重新加载.时间必须使用GMT格式-->
<meta http-equiv="expires" content="Sunday 26 October 2008 01:00 GMT" />

Or set through pragma no-cache, pragma appears in http-equiv In the attribute, use the no-cache value of the content attribute to indicate whether to cache the web page (in order to improve the speed, some browsers will cache the pages browsed by the browser. Through the following definition, the browser generally will not cache the page, and the browser cannot be offline. browse).

<meta http-equiv="pragma" content="no-cache" />

2. For ASP pages:

<%
Response.Buffer=true
Response.CacheControl="no-cache" &#39;禁止代理服务器缓存本页面
Response.Expires=-1000 &#39;让页面立即过期(这儿最好设置一个绝对值较大的负数)
%>

3. For JSP pages:

<%
if(request.getProtocol().compareTo("HTTP/1.0")==0){
	response.setHeader("Pragma","no-cache");
}
if(request.getProtocol().compareTo("HTTP/1.1")==0){
	response.setHeader("Cache-Control","no-cache");
}
response.setDateHeader("Expires",0);
%>

4. For PHP pages:

<?php 
header(&#39;Cache-Control:no-cache,must-revalidate&#39;);   
header(&#39;Pragma:no-cache&#39;);   
?>

Emphasis: For For dynamic pages, the cached code must be placed before any HTML tag output, otherwise an error will occur.

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to set browser not to cache in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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