Home  >  Article  >  Backend Development  >  How to disable page caching in php

How to disable page caching in php

王林
王林Original
2020-08-03 13:29:062716browse

php method to disable page caching: first set the expiration time of the page, such as [header ( " Expires: Mon, 26 Jul 1970 05:00:00 GMT " )]; then set the last update date of the page to On the same day; finally tell the client browser not to use cache.

How to disable page caching in php

# Page caching is sometimes not needed. We can prohibit the browser from caching the page. Disabling page caching can be easily achieved in PHP using the following statement.

(Recommended tutorial: php graphic tutorial)

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 " );
?>

This is very useful for certain pages, such as : Order information and the products under the order, and clear the corresponding product data in the shopping cart. You definitely don’t want the user to reach the last page, have already generated an order, and then click the browser’s return button to return to the previous page.

(Video tutorial recommendation: php video tutorial)

Then add to the order address page:

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

In this way, this page will not be cached, and there will be If the shopping cart is judged to be empty and the user jumps to the empty shopping cart page, then the user clicks the browser to go back, and when he comes back, he will go directly to the shopping cart page.

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!

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
Previous article:php html to ubb problemNext article:php html to ubb problem