Home > Backend Development > PHP Problem > How to prevent browser from using cached pages in php

How to prevent browser from using cached pages in php

coldplay.xixi
Release: 2023-03-03 13:42:01
Original
1958 people have browsed it

php method to prohibit the browser from using cached pages: 1. Set the expiration time of this page, the code is [header (" Expires: Mon Jul 1970GMT ")]; 2. Tell the client browser not to use cache , the code is [header ( Pragma: ].

How to prevent browser from using cached pages in php

php method to prohibit the browser from using cached pages:

In PHP, you can easily use the following statement to disable page caching, but it is difficult to remember and organize it for everyone to use.

The php code is as follows:

The code is as follows:

<?php
// 设置此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可。
header ( " Expires: Mon, 26 Jul 1970 05:00:00 GMT " );
 // 设置此页面的最后更新日期(用格林威治时间表示)为当天,可以强制浏览器获取最新资料
header ( " Last-Modified:" . gmdate ( " D, d M Y H:i:s " ). "GMT " );
 
// 告诉客户端浏览器不使用缓存,HTTP 1.1 协议
 header ( " Cache-Control: no-cache, must-revalidate " );
 
 // 告诉客户端浏览器不使用缓存,兼容HTTP 1.0 协议
header ( " Pragma: no-cache " );
?>
Copy after login

This is very useful for certain pages, such as order information and products under the order, and clearing the corresponding product data in the shopping cart.

You definitely don’t want the user to reach the last page , the order has been generated, and then click the return button of the browser to return to the previous page.

Then add on the order address page:

The code is as follows:

header("Cache-Control:no-cache,must-revalidate,no-store"); //这个no-store加了之后,Firefox下有效
header("Pragma:no-cache");
header("Expires:-1");
Copy after login

This The page is no longer cached, and there is a page that jumps to the empty shopping cart if the items in the shopping cart are empty. Then the user clicks the browser to go back, and when he comes back, he goes directly to the shopping cart page.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to prevent browser from using cached pages in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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