Home > Backend Development > PHP Tutorial > How to Improve Website Performance with HTTP Cache Headers in PHP?

How to Improve Website Performance with HTTP Cache Headers in PHP?

Patricia Arquette
Release: 2024-11-04 11:13:02
Original
576 people have browsed it

How to Improve Website Performance with HTTP Cache Headers in PHP?

How to Implement HTTP Cache Headers for Performance Optimization

In scenarios where dynamic web pages primarily consist of static content, applying HTTP cache headers with PHP can significantly enhance website performance. This guide will delve into the essential headers for effective caching.

Cache Control Directives

cache-control: private, max-age=180 - Sets a private cache for 180 seconds. Private caches are only accessible by individual users, preventing sharing between multiple clients.

cache-control: public, max-age=180 - Sets a public cache for 180 seconds. Public caches are available to all clients, allowing shared access.

HTTP Date and ETag Response Headers

Last-Modified: GMT date and time - Provides the date and time when the content was last modified, allowing browsers to check for updates.

ETag: (unique identifier) - Generates a hash or checksum representing the unique state of the content. Browsers use ETag headers to verify if content has changed since the last request.

Cache Verification Headers

If-Modified-Since: GMT date and time - The client sends the last known modified date of the resource. If there have been no changes since that date, the server responds with a 304 Not Modified status, avoiding unnecessary content retrieval.

If-None-Match: (unique identifier) - The client sends the last known ETag value for the resource. If the ETag matches the server's current value, a 304 Not Modified status is returned.

Implementation with PHP

To implement these headers in PHP, consider the following sample code:

<code class="php">$tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT';
$etag = md5($language . $timestamp);

$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false;

if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) &&
    ($if_modified_since && $if_modified_since == $tsstring)) {
    header('HTTP/1.1 304 Not Modified');
    exit();
} else {
    header("Last-Modified: $tsstring");
    header("ETag: \"{$etag}\"");
}</code>
Copy after login

The above is the detailed content of How to Improve Website Performance with HTTP Cache Headers in 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template