Created
November 18, 2010 20:59
-
-
Save luanlmd/705606 to your computer and use it in GitHub Desktop.
Etag test in PHP
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 | |
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; | |
?> |
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
and suggest
available since PHP 5.4.0