Current Location: Home> Latest Articles> Use header() to control browser cache to improve performance

Use header() to control browser cache to improve performance

M66 2025-05-17

In web development, the performance of the website is crucial. Browser caching is an important factor in improving website performance. Rational use of browser cache can greatly reduce website loading time, improve user experience, and reduce server burden. This article will introduce how to use PHP's header() function to control browser caching and improve website performance by setting appropriate caching strategies.

What is browser caching?

Browser caching is a mechanism that reduces the loading time of users when accessing the same page by storing static resources (such as images, CSS, JavaScript files, etc.) on the user's local device. When the user accesses the same page again, the browser can load resources directly from the cache without having to request it from the server again, thus speeding up page loading.

Use PHP's header() to control browser cache

In PHP, we can use the header() function to set the HTTP header to control the browser's cache behavior. By rationally configuring the cache control header, resource loading can be optimized and redundant network requests can be reduced.

Set cache control header

Here are several common HTTP headers that control browser cache:

  1. Cache-Control : Specifies the cache policy.

  2. Expires : Sets the time when the resource expires.

  3. ETag : Provides a unique identifier for the resource to determine whether the resource has been updated.

  4. Last-Modified : Specifies the time when the resource was last modified.

Sample code

 <?php
// Set cache control header,Tell the browser to cache static resources,And the cache time is one day
header("Cache-Control: max-age=86400, public");

// set up Expires head,set up缓存的过期时间为明天
$expires = gmdate("D, d M Y H:i:s", strtotime("+1 day")) . " GMT";
header("Expires: $expires");

// set up ETag head,Use the resource&#39;s content hash as a unique identifier
$etag = md5(file_get_contents("path/to/your/resource"));
header("ETag: \"$etag\"");

// set up Last-Modified head,set up资源的最后修改时间
$lastModified = gmdate("D, d M Y H:i:s", filemtime("path/to/your/resource")) . " GMT";
header("Last-Modified: $lastModified");
?>

Detailed description

1. Cache-Control

The Cache-Control header can be used to specify the specific policy of the cache, and common options include:

  • max-age=<seconds> : Specifies the maximum time that a resource can be cached in the browser, in seconds. For example, max-age=86400 means that the resource can be stored in the cache for one day.

  • no-cache : Instructs the browser to verify the validity of the resource every time it requests, even if it already has a copy in the cache.

  • no-store : means that no resources are cached and will be re-fetched from the server every time the request is requested.

2. Expires

Expires header sets the expiration time of the resource. The browser will use this time as the basis for determining whether the cache is valid. If the current time exceeds Expires time, the browser will re-request the server to obtain the resource. This time is a specific date and time. The gmdate() function is usually used to generate a time string in GMT format.

 $expires = gmdate("D, d M Y H:i:s", strtotime("+1 day")) . " GMT";
header("Expires: $expires");

3. ETag

An ETag is an identifier generated by the server, which is usually a hash of the resource content. Through ETag , the browser can determine whether the local cached resources have been modified. If the ETag and the ETag returned by the server are the same, the browser can use the local cache, otherwise the resource will be retrieved from the server again.

4. Last-Modified

The Last-Modified header indicates the last modification time of the resource. When the browser sends a request, it will send this time to the server, and the server determines whether the resource has changed based on the modification time. If the resource has not changed, a 304 status code will be returned, telling the browser to use the cache.

How to combine these cache headers to improve performance?

  1. Caching of static resources : For images, CSS, and JavaScript files, you can usually set a longer cache time, such as one day or more. Setting max-age=86400 with Cache-Control , the browser caches these files without requesting the server every time.

  2. Cache of dynamic resources : For dynamic content (such as page HTML), you can use Last-Modified or ETag to ensure that the resource will be reloaded only if it is actually modified. This can effectively reduce unnecessary requests.

  3. Control cache failure : Use Expires to set a reasonable expiration time to ensure that the cache is still valid when it expires and avoid unnecessary resource reloading.

Replace URL domain name

In actual development, we may use the URL of external resources in our code. When the domain name of these resources changes, we can update our cache policy with simple modifications.

For example, suppose there is the following URL:

 $url = "http://example.com/resource";

You can use the following code to replace example.com with m66.net to ensure cache control on the new domain name:

 $url = str_replace("example.com", "m66.net", $url);

In this way, you can easily change the domain name in your code without affecting cache control.

in conclusion

Reasonable caching control is one of the key tips to improve website performance. Through PHP's header() function, we can set different cache headers to help the browser effectively cache resources, thereby improving page loading speed and reducing server pressure. By using Cache-Control , Expires , ETag and Last-Modified , you can ensure that your website is optimized in performance.

Hope this article helps you better understand how to use PHP to control browser caches to improve website performance!