sometimes some static files (such as pictures) will be output by PHP, will find that the request is 200, the static file every time to the server to request too waste of resources, then how to let the browser cache pictures? We need to output 304 in PHP.
We can use the http_if_modified_since in PHP to combine etag to do this. ETag does not specify the format, we can use the file to modify the time of the MD5 value, the code is as follows:
The code is as follows:
Private Function _addetag ($file) {
$last _modified_time = Filemtime ($file);
$etag = Md5_file ($file);
Always Send headers
Header ("Last-modified:" Gmdate ("D, D M Y h:i:s", $last _modified_time). " GMT ");
Header ("Etag: $etag");
Exit If not modified
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
}
}
You can call it before you output it in a static file, such as a picture, in your code.