Skip to content

Instantly share code, notes, and snippets.

@luanlmd
Created November 18, 2010 20:59
Show Gist options
  • Save luanlmd/705606 to your computer and use it in GitHub Desktop.
Save luanlmd/705606 to your computer and use it in GitHub Desktop.
Etag test in PHP
<?php
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Etag test</title>
</head>
<body>
<h1>Etag test</h1>
<p>Your luck number is... <?php echo rand(1,3) ?></p>
<a href="">reload</a>
</body>
</html>
<?php
$data = ob_get_contents();
ob_end_clean();
$etag = md5($data);
header("Etag: {$etag}");
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag))
{
header('HTTP/1.0 304 Not Modified');
exit();
}
echo $data;
?>
@shiny
Copy link

shiny commented Nov 1, 2017

ETag should surrounded by double quote, according to https://tools.ietf.org/html/rfc7232#page-9
Or browser won't recognize it.

header("Etag: \"{$etag} \"");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment