복붙노트

PHP 사이트에 대한 브라우저 캐시를 방지하는 방법

PHP

PHP 사이트에 대한 브라우저 캐시를 방지하는 방법

나는 PHP 서버가 구름 server.When 적 새 파일을 CSS를, js 또는 이미지를 브라우저에 캐시에 저장되어있는 이전 js, css 및 이미지 파일을로드하는 추가 할 수 있습니다.

내 사이트는 아래와 같이 doctype 및 meta 태그를가집니다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Page-Enter" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Page-Exit" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Site-Enter" content="blendTrans(Duration=1.0)">
  <meta http-equiv="Site-Exit" content="blendTrans(Duration=1.0)">

위의 doctype과 메타 코드 때문에 새로운 파일 대신 브라우저에 캐시 된 동일한 파일이로드됩니다.

해결법

  1. ==============================

    1.이 시도

    이 시도

    <?php
    
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    ?>
    
  2. ==============================

    2.HTML을 통해 제어하려면 다음과 같이하십시오. 옵션 1 :

    HTML을 통해 제어하려면 다음과 같이하십시오. 옵션 1 :

    <meta http-equiv="expires" content="Sun, 01 Jan 2014 00:00:00 GMT"/>
    <meta http-equiv="pragma" content="no-cache" />
    

    PHP를 통해 제어하려는 경우 : 아래처럼 수행하십시오. 옵션 2 :

    header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', FALSE);
    header('Pragma: no-cache');
    

    그리고 옵션 2는 항상 프록시 기반 캐싱 문제를 피하기 위해 더 좋았습니다.

  3. ==============================

    3.이것을 시도 할 수 있습니다 :

    이것을 시도 할 수 있습니다 :

        header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        header("Connection: close");
    

    다행히 캐시를 막는 데 도움이됩니다.

  4. ==============================

    4.내 CSS 파일을 캐싱하는 데 문제가있었습니다. PHP에서 헤더를 설정하는 것은 도움이되지 않았습니다. 아마도 헤더가 링크 된 페이지 대신 스타일 시트 파일에 설정되어야하기 때문일 것입니다.

    내 CSS 파일을 캐싱하는 데 문제가있었습니다. PHP에서 헤더를 설정하는 것은 도움이되지 않았습니다. 아마도 헤더가 링크 된 페이지 대신 스타일 시트 파일에 설정되어야하기 때문일 것입니다.

    이 페이지에서 해결책을 찾았습니다 : https://css-tricks.com/can-we-prevent-css-caching/

    해결책:

    timestamp를 링크 된 파일에 대한 URI의 쿼리 부분으로 추가하십시오. (css, js, 이미지 등에 사용될 수 있음)

    개발 용 :

    프로덕션의 경우 (캐싱이 대부분 좋은 점) :

    (필요할 때 수동으로 다시 쓰십시오)

    또는이 두 가지 조합 :

    <?php
        define( "DEBUGGING", true ); // or false in production enviroment
    ?>
    <!-- ... -->
    <link rel="stylesheet" type="text/css" href="style.css?version=3.2<?php echo (DEBUGGING) ? date('_Y-m-d_H:i:s') : ""; ?>">
    

    편집하다:

    또는이 두 가지를 더 예쁘게 조합하십시오.

    <?php
        // Init
        define( "DEBUGGING", true ); // or false in production enviroment
        // Functions
        function get_cache_prevent_string( $always = false ) {
            return (DEBUGGING || $always) ? date('_Y-m-d_H:i:s') : "";
        }
    ?>
    <!-- ... -->
    <link rel="stylesheet" type="text/css" href="style.css?version=3.2<?php echo get_cache_prevent_string(); ?>">
    
  5. ==============================

    5.브라우저 캐시를 방지하는 것은 경우에 따라 좋은 생각이 아닙니다. 해결책을 찾기 위해 다음과 같은 해결책을 찾았습니다.

    브라우저 캐시를 방지하는 것은 경우에 따라 좋은 생각이 아닙니다. 해결책을 찾기 위해 다음과 같은 해결책을 찾았습니다.

    <link rel="stylesheet" type="text/css" href="meu.css?v=<?=filemtime($file);?>">
    

    여기에 문제는 서버의 업데이트 중에 파일을 덮어 쓰는 경우 (내 시나리오) 타임 스탬프가 파일의 내용이 동일하더라도 수정되기 때문에 캐시가 무시된다는 것입니다.

    이 솔루션을 사용하여 콘텐츠가 수정 된 경우에만 브라우저가 자산을 다운로드하도록합니다.

    <link rel="stylesheet" type="text/css" href="meu.css?v=<?=hash_file('md5', $file);?>">
    
  6. from https://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site by cc-by-sa and MIT license