[PHP] Etag

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 18-08 10:15
Hallo,

Ik ben bezig om een script te maken wat via een request bestanden uit een map streamt via header. Ik probeer de bestanden te cachen aan de client side met de ETag en Last Modified header.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?
require_once("../init.php");

//create a string object of it
$url = new String($_GET['file']);

//remove the slash
$url = (substr($url, -1) == "/") ? new String(substr($url, 0, -1)) : $url;

//get the filepath for the file
$file = filepath();

//check if its not empty
if(empty($file))
{
    header("HTTP/1.0 404 Not Found", true, 404);
    
    exit;
}

$size = filesize($file);

header("Cache-Control: must-revalidate");
header("Pragma: hack");
header("Accept-Ranges: bytes");
header(sprintf("Expires: %s", gmdate("D, d M Y H:i:s", gmmktime(gmdate("H")+24)) .' GMT'));
header(sprintf("Content-Length: %d", $size));

//set the header
if($url->endsWith(".css")) header("Content-Type: text/css");
if($url->endsWith(".js")) header("Content-Type: text/javascript");
if($url->endsWith(".gif")) header("Content-Type: image/gif");
if($url->endsWith(".png")) header("Content-Type: image/png");
if($url->endsWith(".jpg")) header("Content-Type: image/jpg");
if($url->endsWith(".jpeg")) header("Content-Type: image/jpeg");
if($url->endsWith(".swf")) header("Content-Type: application/x-shockwave-flash");

doConditionalGet($file);

if(($fp = fopen($file, "r")) !== false)
{
    echo fread($fp, $size);
    
    fclose($fp);

    exit;
}

header("HTTP/1.0 404 Not Found", true, 404);

function doConditionalGet($filepath) {
    // A PHP implementation of conditional get, see 
    //   http://fishbowl.pastiche.org/archives/001132.html
    
    $stats = stat($filepath);

    $etag = sprintf('%x-%x-%x', $stats['ino'], $stats['size'], $stats['mtime']);
    
    $last_modified = gmdate("D, d M Y H:i:s", $stats['mtime']) .' GMT';
    
    // Send the headers
    header("Last-Modified: $last_modified");
    header(sprintf('ETag: "%s"', $etag));
    
    // See if the client has provided the required headers
    $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
                        ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE'])
                        : false;
    $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH'])
                        ? substr(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']), 1, -1)
                        : false;

    if (!$if_modified_since && !$if_none_match) return; // At least one of the headers is there - check them
    if ($if_none_match && $if_none_match != $etag) return; // etag is there but doesn't match
    if ($if_modified_since && $if_modified_since != $last_modified) return; // if-modified-since is there but doesn't match

    // Nothing has changed since their last request - serve a 304 and exit
    header('HTTP/1.0 304 Not Modified');
    exit;
}


//get the filepath
function filepath()
{
    $url = new String($_GET['file']);
    
    if($url->startsWith("styles")) $c = "controllers/" . $url->replace("/styles\//", null)->appendAfterLast("/", "styles/");
    if($url->startsWith("scripts")) $c = "controllers/" . $url->replace("/scripts\//", null)->appendAfterLast("/", "scripts/");

    $paths = array(
        "sites/" . Domain . "/" . $_GET['file'],
        "controllers/" . $_GET['file']
    );

    foreach($paths as $p)
    {
        $file = BasePath . DS . str_replace("/", DS, $p);

        if(file_exists($file)) return $file;
    }

    return null;
}


Nu is het probleem dat op mijn localhost het script goed werkt (soms laad hij jquery.js wel telkens opnieuw), maar als ik het op live zet op: http://www.aeternum-wow.net dan zet hij acther elke ETag "-gzip". Dit maakt het Conditional GET mechanism niet werkend denk ik.

Ik heb via de htaccess zlib compression al uitgezet en ik heb gekeken welke apache modules er worden ingeladen, maar het lijkt er op dat er alleen gebruik wordt gemaakt van mod_deflate. Ik heb ook geprobeerd de ETag te wijzigen naar een md5 hash over de datum, maar dat maakte ook weinig uit.

Iemand die hier ervaring mee heeft?

Acties:
  • 0 Henk 'm!

  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 18-08 10:15
Opgelost:

.htacccess:

php_value zlib.output_compression Off

In bovenstaande PHP code bij het echo'en van het bestand ob_start('ob_gzhandler'); gebrukt