Php static file returns 304, _ PHP Tutorial

Source: Internet
Author: User
Php static files return 304 ,. Php static files return 304. 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, in this case, if the php static file returns 304,

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 useHTTP_IF_MODIFIED_SINCECombine 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:

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 request static files to the server every time, as shown in the following figure...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.