Jump to content

cache control code - questions on script


mihomes

Recommended Posts

Comments on the below please.  Basically this should send out the correct headers to tell if it should retrieve a new version of the page or use the cached version (it has the current).

top of every page goes :

[code]<?php include($_SERVER['DOCUMENT_ROOT']."/cache.php");?>
// sends the gmt last modified date
<?php doConditionalGet(gmdate("D, d M Y H:i:s", filemtime($_SERVER['SCRIPT_FILENAME'])) . " GMT");;?>[/code]

in cache.php is :

[code]<?php
//---------------------------------
// doConditionalGet function definition
//---------------------------------
function doConditionalGet($timestamp) {
  // Create ETag from last modified date
  $etag = '"'.md5($timestamp).'"';

  // Send the headers
  header("Last-Modified: $timestamp");
  header("ETag: $etag");
  header('Expires: '.gmdate('D, d M Y H:i:s', strtotime('+1 month')).' GMT');

  // If browser provided IMS and ETag, get them
  // Otherwise, set variable to 'false'
  $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
      stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
  $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
      stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
  if (!$if_modified_since && !$if_none_match) {
      return; }

  // If at least one value exists, validate them
  if ($if_none_match && $if_none_match != $etag) {
      return; // ETag exists but doesn't match
}

  if ($if_modified_since && $if_modified_since != $timestamp) {
      return; // IMS exists but doesn't match
}

  // Nothing has changed since last request
  // Return a 304 and do NOT run the rest of page
  header('HTTP/1.1 304 Not Modified');
  exit;
}
?>[/code]

Everything appears to work fine, however, I suspect the expires tag is wrong.  Currently it sends the current date/time plus one month.  This SHOULD be one month after the last modifed date.

What would the correct code to replace this so it is '$timestamp + one month'?  $timestamp in its current form would be something such as 'Wed, 27 Dec 2006 07:15:07 GMT'.  Is there any easy way to just add one month to it?
Link to comment
https://forums.phpfreaks.com/topic/32012-cache-control-code-questions-on-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.