Home > Backend Development > PHP Tutorial > How can I effectively implement HTTP caching with PHP for dynamic websites with static content?

How can I effectively implement HTTP caching with PHP for dynamic websites with static content?

Patricia Arquette
Release: 2024-11-04 09:35:01
Original
838 people have browsed it

How can I effectively implement HTTP caching with PHP for dynamic websites with static content?

Integrating HTTP Caching with PHP

In situations where page content remains primarily static but surrounded by a dynamic template, leveraging HTTP cache headers can optimize page delivery. Here's a simplified guide to implementing effective caching using PHP:

Essential Headers for Caching

To enable caching, consider sending the following headers:

  • Content-Type: Specify the MIME type and character set of the response.
  • Vary: Accept: Indicate that the response can vary based on the client's Accept header, controlling content negotiation.
  • Cache-Control: Set directives like private_no_expire to allow private caching but prevent revalidation. Alternatively, use public to allow shared caching.
  • Last-Modified: Provide the date and time when the content was last modified.
  • ETag: Assign a unique identifier to the content, which clients can use to determine if the cached version is still current.

Conditional Requests and Handling

Process incoming conditional requests using if-modified-since and if-none-match:

  • if-modified-since: Compare the request header with the Last-Modified header. If the cached version is still valid, return a 304 (Not Modified) status code.
  • if-none-match: Validate against the ETag header. If the cached version matches, return a 304.

Determining Cache Validity

When generating the ETag, consider using a checksum or a combination of factors like user ID, language, and timestamp. For longer-lasting static content, set a longer expiration.

Example Implementation

<code class="php">$tsstring = gmdate('D, d M Y H:i:s ', $timestamp) . 'GMT';
$etag = $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 &amp;&amp; $if_none_match == $etag) || (!$if_none_match)) &amp;&amp; 
($if_modified_since &amp;&amp; $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 can I effectively implement HTTP caching with PHP for dynamic websites with static content?. 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