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

PHP cleverly uses 304 code to cache image files

WBOY
Release: 2016-07-25 08:52:15
Original
1308 people have browsed it
  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).



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template