How to Use Etags in a PHP File

How to use etags in a PHP file?

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;
}
?>

PHP REST API with ETAG support?

You can find the corresponding request headers in $_SERVER If-Match. Is the key HTTP_IF_MATCH, If-None-Match corresponds to HTTP_IF_NONE_MATCH.

In order to send proper ETags to the client use the ETag header like header( 'ETag: ' . generateEtag() );. You just need to ensure that your ETag represents the response of the service properly.

Grab text between specific tags in PHP file

This is how you would escape the regex:

foreach (glob("template/*.php") as $fn) {

$file = file_get_contents($fn);

preg_match_all("#\{\('(\w+)'\)}#", $file, $matches);

print_r($matches);

}

Eugen has shown how to match the PHP/PI <? tags and /* comment sections as well. You may just need \s* in between those.



Related Topics



Leave a reply



Submit