为什么图像从我的Web服务器提供服务的客户端不缓存? [英] Why do images served from my web server not cache on the client?

查看:131
本文介绍了为什么图像从我的Web服务器提供服务的客户端不缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我店所有的根目录背着我的图像(前 /无功/网络/ ),这意味着web服务器无法缓存头送回了我的照片。我需要什么添加到这个使用户的Web缓存的工作?目前,这是越来越由同一个浏览器中点击每一次。

我的< IMG> 我的网页上的路径看起来像这样:

 < IMG SRC =pic.php U = 1134&放大器; I = 13513&安培; S = 0>

编辑:难道说这是因为<?code> pic.php U = 1134&安培; I = 13513&安培; S = 0 是不是有效的文件名还是什么?

  // pic.php
&LT; PHP    //以二进制方式打开文件
    $ USER = $ _GET ['U'];
    $ n = $ _GET ['我'];
    $ S = $ _GET ['S'];    如果(使用isset($用户)及!&安培;!使用isset($ S)及和放大器; $使用isset($ ID))
    {
    //显示一个锁!
    出口(0);
    }    require_once(bootstrap_minimal.php); //设置数据库连接等    //这是否image_id属于用户?
    $语句= $ DB-GT&;查询(SELECT image_id,USER_ID,FILE_NAME,私人从图像中WHERE image_id =?',$ id)的;
    $ OBJ = $ stmt-&GT; fetchObject();    如果(is_object($ OBJ))
    {
    //是图片是用户?
    如果($ obj-&GT;!user_id说明= $ _SESSION ['USER_ID'])
    {
    //这是一个私人的图片?
    如果($ obj-&GT;民营== 1)
    {
    //检查权限...
    //显示所需的锁!
    }
    }
    }
    其他
    {
    //显示一个错误PIC?
    出口(0);
    }    如果($ S == 0)
    {
    $图片= $共&GT; getImagePathThumb($ obj-&GT; FILE_NAME);
    }
    其他
    {
    $图片= $共&GT; getImagePath($ obj-&GT; FILE_NAME);
    }    //发送正确的头
    标题(内容类型:图像/ PNG);
    标题(内容长度:文件大小($图片));    $计划生育=的fopen($画面,RB);    //转储画面和停止脚本
    fpassthru($ FP);
    出口;
?&GT;


解决方案

您需要添加类似:

  $到期= 3600 * 24 * 7; // 一周
头('过期:'gmdate('D,D M Y H:我:s'的时间()+ $到期),GMT);
标题(缓存控制:私人,最大年龄='$到期);

I store all of my images behind the webroot (before /var/www/), which means that the web server is unable to send cache headers back for my pictures. What do I need to add to this to make the user's web cache work? Currently, this is getting hit every time by the same browser.

My <img> path on my pages look something like this:

<img src="pic.php?u=1134&i=13513&s=0">

Edit: Could it be that it is because "pic.php?u=1134&i=13513&s=0" is not a valid file name or something?

// pic.php
<?php

    // open the file in a binary mode
    $user = $_GET['u'];
    $id = $_GET['i'];
    $s = $_GET['s'];

    if (!isset($user) && !isset($s) && $isset($id))
    {
    	// display a lock!
    	exit(0);
    }

    require_once("bootstrap_minimal.php"); //setup db connection, etc

    // does this image_id belong to the user?
    $stmt = $db->query('SELECT image_id, user_id, file_name, private FROM images WHERE image_id = ?', $id);
    $obj = $stmt->fetchObject();

    if (is_object($obj))
    {
    	// is the picture is the users?
    	if ($obj->user_id != $_SESSION['user_id'])
    	{
    		// is this a private picture?
    		if ($obj->private == 1)
    		{
    			// check permissions...
    			// display a lock in needed!
    		}
    	}
    }
    else
    {
    	// display a error pic?!
    	exit(0);
    }

    if ($s == 0)
    {
    	$picture = $common->getImagePathThumb($obj->file_name);
    }
    else
    {
    	$picture = $common->getImagePath($obj->file_name);
    }

    // send the right headers
    header("Content-Type: image/png");
    header("Content-Length: " . filesize($picture));

    $fp = fopen($picture, 'rb');

    // dump the picture and stop the script
    fpassthru($fp);
    exit;
?>

解决方案

You need to add something like:

$expiry = 3600*24*7; // A week
header('Expires: ' . gmdate('D, d M Y H:i:s' time() + $expiry) . ' GMT');
header('Cache-control: private, max-age=' . $expiry);

这篇关于为什么图像从我的Web服务器提供服务的客户端不缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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