PHP static file return_PHP tutorial

WBOY
Release: 2016-07-13 09:55:30
Original
846 people have browsed it

php static files return

Sometimes some static files (such as pictures) will be output by php, and you will find that the request is 200, and the static files go to the server every time The request is too wasteful of resources. How to let the browser cache the image? We need to output 304 in php.

We can use HTTP_IF_MODIFIED_SINCE in php combined with etag 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;

 }

 }

It can be called in the code before static files (such as pictures) are output.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991645.htmlTechArticlephp static file return Sometimes some static files (such as pictures) will be output by php, and you will find that the requests are all 200, It is a waste of resources to request static files from the server every time. How to make the browser...
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!