Home > Backend Development > PHP Tutorial > How Can I Prevent Browser Caching of Assets Served Through PHP?

How Can I Prevent Browser Caching of Assets Served Through PHP?

DDD
Release: 2024-12-11 09:58:10
Original
326 people have browsed it

How Can I Prevent Browser Caching of Assets Served Through PHP?

Avoiding Browser Caching of Assets Requested Through PHP Pages

When updating your site's CSS, JS, or image files, you may notice that the browser continues to display the old, cached versions. Here's how to prevent this issue when serving pages through PHP:

Using HTTP Headers

To prevent the browser from caching assets requested through PHP pages, modify your PHP code to add the following HTTP headers:

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

?>
Copy after login

These headers instruct the browser:

  • Not to store the asset in its cache.
  • Not to use a cached version of the asset, even if it is older than the current version.
  • Not to perform any caching checks before requesting the asset from the server.
  • Not to cache the asset even if it is requested multiple times.

By adding these headers to your PHP code, the browser will be forced to fetch the latest version of the asset every time it is requested, ensuring that visitors always receive the most up-to-date version.

The above is the detailed content of How Can I Prevent Browser Caching of Assets Served Through PHP?. For more information, please follow other related articles on the PHP Chinese website!

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