缓存图像通过PHP请求 - 如果-Modified-Since的送交 [英] Caching image requests through PHP - If-Modified-Since not being sent

查看:143
本文介绍了缓存图像通过PHP请求 - 如果-Modified-Since的送交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我服通过PHP的图像和有一些问题设置它与304头回应,以节省加载时间。

大部分code下面我就php.net找到。它的工作原理,与200的由于某些原因,如果-Modified-Since的没有被上即使我最初发送Last-Modified头的任何请求接收的报头。这是正在做的不过总是响应Apache服务器。任何想法可能是错误的?

例子在这里。

这个页面将从磁盘加载图像并显示给浏览器,以发送Last-Modified头一起。如果刷新页面,浏览器不发送的If-Modified-Since头像它应该。

 定义('SITEPATH',(目录名($ _ SERVER ['SCRIPT_NAME'])=='/')'/':目录名($ _ SERVER ['SCRIPT_NAME']) '/');$ LOAD_PATH = $ _ SERVER ['DOCUMENT_ROOT']。 SITEPATH。 fpo_image.jpg';//获取客户端发送的报头。
$头= apache_request_headers();
$ file_time = filemtime($ LOAD_PATH);标题(缓存控制:必重新验证');
头('的Last-Modified:.gmdate('D,D M Y H:I:S',$ file_time)GMT);如果(使用isset($标题[如果-Modified-Since的'])及及(的strtotime($标题[如果-Modified-Since的'])== $ file_time)){    标题(HTTP / 1.1 304未修改);
    标题(连接:关闭');}其他{    首标('HTTP / 1.1 200 OK);
    标题(内容长度:文件大小($ LOAD_PATH));
    标题(内容类型:image / JPEG');    ReadFile的($ LOAD_PATH);}


解决方案

我相信它应该是

 如果(使用isset($标题[如果-Modified-Since的'])及及(的strtotime($标题[如果-Modified-Since的'])> = $ file_time)){

检查,如果修改时间大于或等于,而不是仅仅相等。虽然我明白这两个值的的是相同的。

I am serving images through php and having some problems setting it up to respond with 304 headers to save on load time.

Most of the code below I found on php.net. It works, however ALWAYS responds with 200. For some reason the If-Modified-Since header is not being received on any requests even though I am sending the Last-Modified header initially. This is being done on an apache server. Any idea what might be wrong?

Example here.

This page will load the image from disk and display it to browser, along with sending a Last-Modified header. If you refresh the page, the browser doesn't send a If-Modified-Since header like it should.

define('SITEPATH', (dirname($_SERVER['SCRIPT_NAME']) == '/') ? '/' : dirname($_SERVER['SCRIPT_NAME']).'/');

$load_path = $_SERVER['DOCUMENT_ROOT'] . SITEPATH . 'fpo_image.jpg';

// Get headers sent by the client.
$headers    = apache_request_headers(); 
$file_time  = filemtime($load_path);

header('Cache-Control: must-revalidate');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $file_time).' GMT');

if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $file_time)) {

    header('HTTP/1.1 304 Not Modified');
    header('Connection: close');

} else {

    header('HTTP/1.1 200 OK');
    header('Content-Length: '. filesize($load_path));
    header('Content-type: image/jpeg');                         

    readfile($load_path);

}

解决方案

I believe it should be

if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) >= $file_time)) {

Checking if the modified time is greater than or equal rather than just equal. Although I do understand the two values should be the same.

这篇关于缓存图像通过PHP请求 - 如果-Modified-Since的送交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆