Configuring PHP Etag Reliably

im unable to correctly configure Etag on user browser. When a user clicks on one of my external links, I would like to set the article identifier in my Etag (I also use cookies, but id would like to experiment with Etag specifically to check its reliability).

When the same user returns to my site in a few hours / days, I would like to read the Etag value and use it for others.

I can set Etag to the initial click, but when the user returns the Etag value, it disappeared. I guess it expired or something else. Here is the code I tried:

<? $time = 1280951171; $lastmod = gmdate('D, d MYH:i:s \G\M\T', $time); $etag = '123'; $ifmod = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod : null; $iftag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag : null; if (($ifmod || $iftag) && ($ifmod !== false && $iftag !== false)) { header('HTTP/1.0 304 Not Modified'); } else { header("Last-Modified: $lastmod"); header("ETag: $etag"); } print_r($_SERVER); ?> 
+6
php header etag
source share
2 answers

You should wrap your etag in double quotes (as mentioned in the Codler link):

 '"' . $etag . '"' 

I don’t think this can solve your problem, but you probably want

 header('Not Modified',true,304); 

instead

 header('HTTP/1.0 304 Not Modified'); 

In PHP 5.4, there is a better way to do this using http_response_code :

 http_response_code(304); 

Also, have you checked that ordinary suspects stop the headers? The Unicode Byte-Order marker is very annoying. (Do not pay attention if you can see other headers that you customize yourself)

+5
source share
-3
source share

All Articles