Skip to content

Instantly share code, notes, and snippets.

@yama
Created March 20, 2018 23:27
Show Gist options
  • Save yama/0bdccde62afb005a86161b9bc6514341 to your computer and use it in GitHub Desktop.
Save yama/0bdccde62afb005a86161b9bc6514341 to your computer and use it in GitHub Desktop.
条件付きGET
<?php
class CGET {
function __construct($timestamp=0, $content='') {
if(!$timestamp) return;
$last_mod = gmdate('D, d M Y H:i:s T', $timestamp);
if(!$content) $content = $last_mod;
$etag = md5($content);
header('Last-Modified: '.$last_mod);
header(sprintf('ETag: "%s"', $etag));
if(!$this->isNotModified($last_mod) || !$this->isMatchContent($etag)) return;
header('HTTP/1.1 304 Not Modified');
header('Content-Length: 0');
exit;
}
function isNotModified($last_mod) {
if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) return false;
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_mod) return true;
return false;
}
function isMatchContent($etag) {
$_ = $_SERVER;
if (!isset($_['HTTP_IF_NONE_MATCH'])) return false;
if (trim($_['HTTP_IF_NONE_MATCH'],'"') === $etag) return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment