简体   繁体   English

Apache / PHP缓存头问题

[英]Apache/PHP cache headers problem

<?php
    chdir('../../../../');
    include('bootstrap.php');

    $place = isset($_GET['place'])  ?   (is_array($_GET['place']))   ?   intval($_GET['place'][0]) :   intval($_GET['place'])      : null;

    $query = mysql_query("SELECT `place`, `image`, `imagetype` FROM `topvideo` WHERE `place` = '" . $place . "'");
    if (mysql_num_rows($query))
    {
        while ($row = mysql_fetch_array($query))
        {
            $im = imagecreatefromstring($row['image']);
            if ($im !== false)
            {
                header('Cache-Control: public, proxy-revalidate');
                header('Last-Modified:Mon, 02 Nov 2009 09:50:18 GMT');
                header('Expires: ' . gmdate ("D, d M Y H:i:s", time() + 60 * 60 * 24 * 24 . ' GMT');

                header('Content-Type: image/jpeg');
                imagejpeg($im);
                imagedestroy($im);
            }
        }
    }

The response is always 200, but I need to cache the image for 2 days and the response was 304.... why? 响应始终为200,但是我需要将图像缓存2天,响应为304 ....为什么? sorry for bad english 对不起,英语不好

to send 304 You need to include Etag in your response headers, etag is something like hash of content itself and date of it's creation. 发送304您需要在响应标头中包含Etag,etag类似于内容本身的哈希值及其创建日期。

After you include etag in response the browser will send you request header "If-None-Match". 在响应中包含etag之后,浏览器将向您发送请求标头“ If-None-Match”。

You have to compare this headers and if they match respond with 304, if not send new content with new Etag header and status 200. 您必须比较此标头,如果它们匹配则用304响应,如果没有发送带有新Etag标头和状态200的新内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM