PHP Cache-control: Header Function Guide

In PHP, the header function can be used to set the Cache-control parameter in the response header. This parameter is used to control browser caching behavior. Below are some commonly used Cache-control parameters and their usage methods:

  1. public: Indicates that the response can be stored by any cache.
header("Cache-control: public");
  1. Private: specifies that the response can only be cached by a single user, typically used for pages with personalized user information.
header("Cache-control: private");
  1. No-cache: Specifies that the browser must send a request to the server for validation before using the cache.
header("Cache-control: no-cache");
  1. no-store: specifies that the browser should not cache the response content.
header("Cache-control: no-store");
  1. The directive “must-revalidate” instructs the browser to send a request to the server for verification before using a cached response.
header("Cache-control: must-revalidate");
  1. max-age: specifies the maximum time (in seconds) that a response can be cached.
header("Cache-control: max-age=3600"); // 缓存1小时
  1. s-maxage: Similar to max-age, but only applicable to shared caches (such as CDNs).
header("Cache-control: s-maxage=3600"); // 缓存1小时
  1. no-transform directive instructs the browser not to manipulate the response content, such as compressing it.
header("Cache-control: no-transform");

You can choose the appropriate Cache-control parameter to control the behavior of browser caching based on your actual needs. Make sure to call the header function to set the response headers before any output.

bannerAds