Created
August 14, 2011 00:26
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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