Php static files return 304 tips for sharing ,. Php static files return 304 tips for sharing. sometimes some static files (slices) are output by php and the requests are all 200, it is a waste of resources to request static files on the server every time. php static files return 304 tips,
Sometimes some static files (slices) are output by php, and all requests are 200. every time a static file is sent to the server, it is a waste of resources. how can we cache images in the browser? We need to output 304 in php.
We can use HTTP_IF_MODIFIED_SINCE and etag in php to do this. Etag does not have a clearly defined format. we can use the md5 value of the file modification time. 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;
}
}
In the code, you can call it before the static file (slice) is output.
Sometimes some static files (fragments) are output by php, and the requests are all 200. it is a waste of resources to send requests to the server for each static file...