3

如何将过期标头放在超出 JS 文件的 PHP 文件上?

.htaccess

ExpiresActive on
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType application/javascript A29030400

JS文件头

Cache-Control   max-age=29030400
Connection  Keep-Alive
Date    Thu, 18 Oct 2012 09:23:16 GMT
Etag    "300000002c8ba-15f-4cc3069c72d00"
Expires Thu, 19 Sep 2013 09:23:16 GMT
Keep-Alive  timeout=5, max=94
Server  Apache/2.2.22 (Win32) PHP/5.4.3

输出 JS 文件的 PHP 文件的标头

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Length  1195
Content-Type    application/javascript
Date    Thu, 18 Oct 2012 09:23:16 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=5, max=100
Pragma  no-cache
Server  Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By    PHP/5.4.3

代码 PHP 文件

<?php
header('Content-Type: application/javascript');
$js_output = array('something' => 'some value');
?>
var <?=json_encode($js_output)?>
4

3 回答 3

2

您可以设置缓存标头:

<?php
header('Content-Type: application/javascript');
header("Cache-Control: max-age=29030400");
header("Cache-Control: public", false); 
$js_output = array('something' => 'some value');
?>
var <?=json_encode($js_output)?>

另见: http: //php.net/manual/en/function.header.php

于 2012-10-20T18:06:10.133 回答
0

此解决方案将返回正确的 304 响应并停止 php 执行,从而允许客户端使用缓存的副本。您不能使用 mod_rewrite 在 htaccess 中完全做到这一点,因为 php 处理器总是会妨碍您。

.htaccess

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType application/javascript   "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
    Header append Cache-Control "public"
</IfModule>

例子.js.php

<?php
header('Content-Type: application/javascript');

if (false === ($timestamp = getlastmod())) {
    if (false === ($timestamp = @filemtime(__FILE__))) {
        if (false === ($timestamp = @filemtime($_SERVER['SCRIPT_FILENAME']))) {
            trigger_error('Failed to retrieve timestamp', E_USER_ERROR);
        }
    }
}
$timestamp_string = gmdate('r', $timestamp);

// Check if the client has the same page cached
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) &&
    ($_SERVER["HTTP_IF_MODIFIED_SINCE"] == $timestamp_string)) {
    header("HTTP/1.1 304 Not Modified");
    exit;
}

// Inform the user agent what is our last modification date
header("Last-Modified: ".$timestamp_string);

$js_output = array('something' => 'some value');
echo 'var json = '.json_encode($js_output).PHP_EOL;
exit;
?>

第一个 GET 响应

HTTP/1.1 200 OK
Date: Wed, 24 Oct 2012 16:20:05 GMT
Server: Apache/2.2.16 (Win32) PHP/5.3.3
X-Powered-By: PHP/5.3.3
Last-Modified: Wed, 24 Oct 2012 16:16:34 GMT
Cache-Control: max-age=31536000
Expires: Thu, 24 Oct 2013 16:20:05 GMT
Content-Length: 39
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/javascript

第二个和后续的 GET 响应

HTTP/1.1 304 Not Modified
Date: Wed, 24 Oct 2012 16:21:31 GMT
Server: Apache/2.2.16 (Win32) PHP/5.3.3
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Expires: Thu, 24 Oct 2013 16:21:31 GMT
Cache-Control: max-age=31536000
于 2012-10-24T16:26:55.450 回答
0

您需要在 .htaccess 中保持几乎相同的 Expire 设置,即:

ExpiresActive on
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType application/javascript A29030400
ExpiresDefault "access plus 1 month"

然后在编写 JS 文件的 PHP 代码中使用正确的Content-Type和:

<?php
header('Content-Type: application/javascript');
exitIfNotModifiedSince(setLastModified());

function setLastModified($lastModified=NULL) {
    $pageModified = getlastmod();

    if(empty($lastModified) || ($lastModified < $pageModified))
        $lastModified = $pageModified;
    $headerModified = filemtime(__FILE__);
    if($headerModified > $lastModified)
        $lastModified = $headerModified;
    header('Last-Modified: ' . date("r",$lastModified));
    return $lastModified;
}

function exitIfNotModifiedSince($lastModified) {
    if(array_key_exists("HTTP_IF_MODIFIED_SINCE",$_SERVER)) {
        $ifModifiedSince = strtotime(preg_replace('/;.*$/', '',
                                     $_SERVER["HTTP_IF_MODIFIED_SINCE"]));
        if($ifModifiedSince >= $lastModified) { 
            header("HTTP/1.0 304 Not Modified");
            exit();
        }
    }
}
$jsData  = array('foo' => 'bar');
?>
var <?=json_encode( $jsData );?>

在顶部。在 Firebug 中监控您的标题,它应该会显示如下内容:

HTTP/1.1 304 Not Modified
Cache-Control:max-age=29030400
Connection:close
Date:Wed, 24 Oct 2012 19:10:43 GMT
Expires:Wed, 25 Sep 2013 19:10:43 GMT
Server:Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.8h DAV/2 PHP/5.2.6
Content-Length: 31
Content-Type: application/javascript
于 2012-10-23T10:48:45.067 回答