How to disable page caching in php

藏色散人
Release: 2023-03-01 08:28:02
Original
3110 people have browsed it

How to disable page caching in php

#How to disable page caching in php?

Page caching is sometimes not needed. We can prevent the browser from caching the page.

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

The php 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 and have already generated an order, and then click the browser’s return button to return to the previous page.

Then add on the order address page:

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

This page is not cached, and there is a page that will jump to the empty shopping cart if the items in the shopping cart are empty, then the user clicks on the browser Go back and come back, and you will go directly to the shopping cart page.

Related recommendations: "PHP Tutorial"

The above is the detailed content of How to disable page caching in php. For more information, please follow other related articles on the PHP Chinese website!

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