duancan1950 2012-04-11 20:31
浏览 24
已采纳

缓存1个多域的文件

First of all, I'm sorry about the title. I couldn't find a better one.

I've a image file, generated by a PHP script. this script (image) is connected to a database and saves its referrer url in a table. Because the output image doesn't change, I think it's better to cache it.

But as I know, if I cache 1 file (for example http://www.example.com/img.png.php), on every pages, the browsers reads it from cache. and it's not good for my script. because on the first call, it save the referrer url and cached by browser. And on the next calls, on different websites (referrers), cached version will be used and browser don't send any request to the server, and finally referrer url won't save in the database.

Can I say to browser, please cache 1 copy of the image for each domain? I mean:

http://wwww.abc.com/index.html sends a request to get my image (script)

browser checks its cache, and doesn't find it. so get it from the server. and PHP script saves the referrer url.

the user goes to another page of ABC.COM. (for example: http://wwww.abc.com/about.html) browser check the cache, it finds it. so doesn't send a request to the server to get the file content. and PHP script won't run.

another site (http://wwww.efg.com/index.html) sends a request to get my image (script) browser checks cache, and WILL NOT find it. so send a request for file content. and PHP script runs...............................

Is it possible? (sorry for long text, with a lots of grammatical problems)

  • 写回答

3条回答 默认 最新

  • doudui2229 2012-04-11 20:39
    关注

    You could use a redirect page (that is not cached) that saves the referrer to your database and then redirects to the cached image.

    That way you always get a hit but the actual image is cached.


    In your HTML you could use:
    <img src="/image.php">
    

    And in image.php:

    <?php
        // save the referrer in here
    
        header('Location: /image.jpg');
    ?>
    

    and /image.jpg is your actual image (which can be cached)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line