php Disable Browsers from Using Cached Pages

  • 2021-07-26 07:14:58
  • OfStack

This article illustrates how php disables browsers from using cached pages. Share it for your reference. The specific methods are as follows:

Page caching is sometimes unnecessary, so we can prohibit browsers from caching pages.

In PHP can easily use the following statement to prohibit page caching, but it is difficult to remember the special arrangement, which is convenient for everyone to use.
The php code is as follows:

<?php
// Set the expiration time of this page ( Expressed in Greenwich Mean Time ) , as long as it is a past date.
header ( " Expires: Mon, 26 Jul 1970 05:00:00 GMT " );
 // Set the last update date of this page ( Expressed in Greenwich Mean Time ) For the same day, you can force the browser to get the latest information
header ( " Last-Modified:" . gmdate ( " D, d M Y H:i:s " ). "GMT " );
 
// Tells the client browser not to use caching, HTTP 1.1 Agreement
 header ( " Cache-Control: no-cache, must-revalidate " );
 
 // Tells client browsers not to use caching, compatible HTTP 1.0 Agreement
header ( " Pragma: no-cache " );
?>

This is useful for certain pages, such as: order information and items placed in an order, and emptying the item data corresponding to the shopping cart.
I definitely don't want the user to go to the last page and have already generated an order. Click the browser's Return button to return to the previous page.
Then add:
header("Cache-Control:no-cache,must-revalidate,no-store"); // This no-store After adding it, Firefox Lower effective 
header("Pragma:no-cache");
header("Expires:-1");

This page is not cached, and there is a page that judges that the shopping cart is empty and jumps to the empty shopping cart, so the user clicks the browser to go back, and when he comes back, he also goes directly to the shopping cart page.

I hope this article is helpful to everyone's PHP programming.


Related articles: