【问题标题】:Is there a way to cache only images only on one webpage?有没有办法只在一个网页上缓存图片?
【发布时间】:2016-10-23 07:09:27
【问题描述】:

我只想在我网站的特定页面上缓存图片。

不缓存所有图像非常重要,所以我想一次做一页。

我可以使用 META TAGS 或 PHP 或其他方法吗?

【问题讨论】:

    标签: caching meta-tags


    【解决方案1】:

    这段代码应该可以工作。

    <?php
    //Caching
    
    //get the last-modified-date of this very file
    $lastModified=filemtime(__FILE__);
    //get a unique hash of this file (etag)
    $etagFile = md5_file(__FILE__);
    //get the HTTP_IF_MODIFIED_SINCE header if set
    $ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
    //get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
    $etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
    
    //set last-modified header
    header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
    //set etag-header
    header("Etag: $etagFile");
    //make sure caching is turned on
    header('Cache-Control: public');
    
    //check if page has changed. If not, send 304 and exit
    if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile)
    {
           header("HTTP/1.1 304 Not Modified");
           exit;
    }
    
    //your normal code
    //echo "This page was last modified: ".date("d.m.Y H:i:s",time());
    
    //END OF Caching
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2017-08-27
      • 1970-01-01
      相关资源
      最近更新 更多