PHP Static file return 304

Source: Internet
Author: User
Tags php tutorial
Sometimes some static files (slices) will be output by PHP, will find that the request is 200, static files each time to go to the server to request too much waste of resources, then how to let the browser cache pictures? We need to output 304 in PHP.

We can use the HTTP_IF_MODIFIED_SINCE combined ETag in PHP to do this. The 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  ; }}

You can call it before the static file (slice) output in your code.

The above describes the PHP static file return 304, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.