Skip to content

Instantly share code, notes, and snippets.

@johnlettman-old
Created August 14, 2011 00:26
Show Gist options
  • Save johnlettman-old/1144424 to your computer and use it in GitHub Desktop.
Save johnlettman-old/1144424 to your computer and use it in GitHub Desktop.
Status Code 304 manipulation for PHP. Allows a dynamically controlled "Last Modified" header for dynamic content, thus allowing for performance.
<?php
$_HEADERS = apache_request_headers(); # Get client headers.
$updateTime = strtotime('yesterday'); # Makes sure that it always caches.
/*
Mock page content.
Change this after first view. If refreshing
shows the old content, then the test worked!
*/
$content = 'test!';
if(isset($_HEADERS['If-Modified-Since']) && (strtotime($_HEADERS['If-Modified-Since']) == $updateTime)) {
// Unmodified!
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $updateTime).' GMT', true, 304);
exit;
} else {
// Modified or unsupported!
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $updateTime).' GMT', true, 200);
echo $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment