Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 缓存动态生成的图像_Php_Browser Cache - Fatal编程技术网

Php 缓存动态生成的图像

Php 缓存动态生成的图像,php,browser-cache,Php,Browser Cache,我正在使用PHP脚本生成一些图像。 我想缓存图像,这样浏览器就不必每次都加载它们。 我添加了以下标题: 'max-age' => 3600 'Etag' => md5($image->getSlug()) 'last-modified' => $image->getUpdatedAt() 'Vary' => 'Accept-Encoding' 'content-length' => (size of the image) 'Content-type' =

我正在使用PHP脚本生成一些图像。
我想缓存图像,这样浏览器就不必每次都加载它们。 我添加了以下标题:

'max-age' => 3600
'Etag' => md5($image->getSlug())
'last-modified' => $image->getUpdatedAt()
'Vary' => 'Accept-Encoding'
'content-length' => (size of the image)
'Content-type' => 'image/jpeg'
但图像不会被缓存,并且每次都由浏览器加载

响应头如下所示(使用Firebug):


有人知道这里出了什么问题吗?

您需要计算并添加一个Expires:header。例如:

Expires: Fri, 30 Oct 2011 14:19:41 GMT
<?php
error_reporting(0); //disable error reporting as it will corrupt the image
session_start(); //start a php session
$date = date(DATE_RFC822);
//some code to connect to a mysql database
$query = mysqli_query($link, "SELECT * FROM images WHERE sessionid='$sessionid'");
if ($query and mysqli_num_rows($query) == 1) { //if mysql returned one row
    $row = mysqli_fetch_array($query); //get the row as an array
    $date = $row["date"]; //set the date to the one in the database
} else { //if the user has never seen the image before
    mysqli_query($link, "INSERT INTO images SET sessionid='$sessionid', date='$date'"); //put the session id and date into the database for later use
}
header("Last-Modified: $date");
//some code that would generate and send the image

强制缓存图像的一种方法是将
Last Modified
标记设置为创建要使用的图像的日期和时间。例如:

Expires: Fri, 30 Oct 2011 14:19:41 GMT
<?php
error_reporting(0); //disable error reporting as it will corrupt the image
session_start(); //start a php session
$date = date(DATE_RFC822);
//some code to connect to a mysql database
$query = mysqli_query($link, "SELECT * FROM images WHERE sessionid='$sessionid'");
if ($query and mysqli_num_rows($query) == 1) { //if mysql returned one row
    $row = mysqli_fetch_array($query); //get the row as an array
    $date = $row["date"]; //set the date to the one in the database
} else { //if the user has never seen the image before
    mysqli_query($link, "INSERT INTO images SET sessionid='$sessionid', date='$date'"); //put the session id and date into the database for later use
}
header("Last-Modified: $date");
//some code that would generate and send the image

如果你看我原来的帖子,你会发现我已经包括了上次修改的内容。@Ako啊,是的,对不起,我的不好。嗯,我想不出还有什么可以做的,除了可能使用“file\u put\u contents”保存图像的内容,然后在显示图像的页面上使用图像文件名。你知道吗?我也有同样的问题。。