I'm trying to reduce my server load by providing 304 Not Modified
responses for content where appropriate. CloudFlare is my middle man, so they should be sending If-Modified-Since
headers whenever a cached page has expired, right?
I'm receiving these responses, client-side:
CF-Cache-Status: MISS
on first page loadCF-Cache-Status: Hit
on page reloads for 20 secondsCF-Cache-Status: EXPIRED
on page reload after 20 seconds
The expired request is forwarded to my server, but doesn't include an If-Modified-Since
header. How can I get this to work?
<?php
$now = time();
header( "ETag: W/\"$now\"" );
header( 'Expires: '.gmdate('D, d M Y H:i:s \G\M\T', $now + 20) );
header( 'Last-Modified: '.time() );
header( 'Cache-Control: public, max-age=20' );
print('<pre>');
print_r($_SERVER);
print('</pre>');
UPDATE: Here's working code
<?php
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// $date = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
header("HTTP/1.1 304 Not Modified");
exit();
}
$format = 'D, d M Y H:i:s \G\M\T';
$now = time();
$date = gmdate($format, $now);
header('Date: '.$date);
header('Last-Modified: '.$date);
$date = gmdate($format, $now+30);
header('Expires: '.$date);
header('Cache-Control: public, max-age=30');
print('<pre>');
print_r($_SERVER);
print('</pre>');