How do you implemented etags inside a PHP file? What do I upload to the server and what do I insert into my PHP file?
An ETag is a unique identifier for a resource URI.
To use ETag for optimistic concurrency, we need to use the If-Match header. While sending a request to change the state (e.g. PUT), we should send the expected state version as the value of the If-Match header. The server should check if the ETag value is equal to the current one. If it equals, save succeed.
The ETag is an identifier assigned to a data resource in a server, and if that resource is ever updated at the server, the ETag is changed. Whenever a resource is requested (via its URL), the data and ETag are retrieved and stored in the Web cache, and the ETag is sent along with subsequent requests.
The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed.
Create / edit your .htaccess file and add the following:
FileETag MTime Size
Either place the following inside a function or put it at the top of the PHP file that you need etags to work on:
<?php $file = 'myfile.php'; $last_modified_time = filemtime($file); $etag = md5_file($file); header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); header("Etag: $etag"); if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { header("HTTP/1.1 304 Not Modified"); exit; } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With