代码之家  ›  专栏  ›  技术社区  ›  DanTdr

如何为不在服务器上的脚本添加过期标头?

  •  20
  • DanTdr  · 技术社区  · 17 年前

    我有一个网站,我在所有页面/图像和脚本上添加了过期标题,但我不知道如何将过期标题添加到外部脚本中。

    例如,谷歌分析-它的过期标题设置为1天。

    9 回复  |  直到 17 年前
        1
  •  22
  •   Gumbo    17 年前

        2
  •  21
  •   raspi    17 年前

    <script type="text/javascript" src="http://external.example.com/foo.js"></script>
    

    <script type="text/javascript" src="external.php?url=http://external.example.com/foo.js"></script>
    

    external.php类似于

    <?php
    header("Expire-stuff: something");
    echo file_get_contents($_GET['url']);
    

    $files = array('foo.js' => 'http://external/...');
    if(isset($files[$_GET['file']]))
    {
      echo file_get_contents($files[$_GET['file']]);
    }
    

    file_get_contents()当然会占用一些带宽,因此建议也缓存结果。

        3
  •  2
  •   echox Petter Nordlander    17 年前

    那是不可能的。

        4
  •  2
  •   Joost    17 年前

    <?php
    header('expire-header');
    
    echo file_get_contents('http://www.extern.al/website/url');
    
        5
  •  1
  •   CoderCat    9 年前

    <?php
    
    $files = array(
        'ga.js' => 'https://ssl.google-analytics.com/ga.js',
        'bsa.js' => 'https://s3.buysellads.com/ac/bsa.js',
        'pro.js' => 'https://s3.buysellads.com/ac/pro.js'
    );
    
    if(isset($files[$_GET['file']])) {
        if ($files[$_GET['file']] == 'ga.js'){
            header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + ((60 * 60) * 48))); // 2 days for GA
        } else {
            header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // Default set to 1 hour
        }
    
        echo file_get_contents($files[$_GET['file']]);
    }
    
    ?>
    

    https://www.catswhocode.com/blog/php-how-to-add-expire-headers-for-external-scripts

        6
  •  1
  •   Avni Yayin    7 年前

        7
  •  0
  •   Linus    17 年前

        8
  •  -4
  •   Php Dev    10 年前

    ExpiresActive On
    
    ExpiresDefault "access plus 1 seconds"
    
    ExpiresByType image/x-icon "access plus 2692000 seconds"
    
    ExpiresByType image/jpeg "access plus 2692000 seconds"
    
    ExpiresByType image/png "access plus 2692000 seconds"
    
    ExpiresByType image/gif "access plus 2692000 seconds"
    
    ExpiresByType application/x-shockwave-flash "access plus 2692000 seconds"
    
    ExpiresByType text/css "access plus 2692000 seconds"
    
    ExpiresByType text/javascript "access plus 2692000 seconds"
    
    ExpiresByType application/x-javascript "access plus 2692000 seconds"
    
    ExpiresByType text/html "access plus 600 seconds"
    
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
    

        9
  •  -10
  •   David Leston David Leston    17 年前

    推荐文章