Home  >  Article  >  Backend Development  >  PHP cleverly uses 304 code to cache image files

PHP cleverly uses 304 code to cache image files

WBOY
WBOYOriginal
2016-07-25 08:52:151239browse
  1. private function _addEtag($file) {
  2. $last_modified_time = filemtime($file);
  3. $etag = md5_file($file);
  4. // always send headers
  5. header("Last-Modified: ".gmdate ("D, d M Y H:i:s", $last_modified_time)." GMT");
  6. header("Etag: $etag");
  7. // exit if not modified
  8. if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE ']) == $last_modified_time ||
  9. @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
  10. header("HTTP/1.1 304 Not Modified");
  11. exit;
  12. }
  13. }
Copy the code

Just call it before outputting static files (such as pictures).



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn