Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use etags in a PHP file?

Tags:

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?

like image 854
Nagra Avatar asked Nov 02 '12 14:11

Nagra


People also ask

What is ETag PHP?

An ETag is a unique identifier for a resource URI.

How do you use an ETag?

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.

Where are Etags stored?

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.

What is Etags caching?

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.


1 Answers

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;  }  ?> 
like image 79
Nagra Avatar answered Oct 15 '22 07:10

Nagra