douzhi2760 2012-10-03 12:33
浏览 78

如何检测浏览器是否从服务器请求某个文件

Can I detect if the web browser is requesting a certain image from the server? I want to check if the user downloads the image or if it is already cached from its browser.

The main idea:

I am counting unique visitors per profile page. I use IPs and Cookies for now but want to add this, too. IP could be changed easily, Cookie could be blocked/deleted. My idea is to use this information just like a flash cookie. The image will be 1px x 1px in size and will be invisible to the user. I don't have experience with ActionScript and Flash at all, so I can't use flash cookie and want to try with this.

EDIT: As I understand from Sven's answer maybe I couldn't explain what I need. My question is same as Sven's answer. How to wait for the request to appear on the server? I want the browser to cache the image, so it will be downloaded only if the user is an unique visitor, i.e. he is viewing the page for the very first time.

I want to get this information and check if the image is requested or not (i.e. it is cached). Something like:

$requested_files = $_SERVER['REQUESTS']; // Or something similar, this is the question.
$file_name = $profile_page_owner_id.'.png'; // For example.

if(in_array($file_name, $requested_files)) {
    // File is requested, so it is not cached. This is an unique visitor.
    // Of course except this I will continue to check IP and Cookie.
    // This will be the 3rd check.
} else {
    // File is not requested, so it is already cached.
    // Page is viewed before, this is not an unique visitor.
}
  • 写回答

3条回答 默认 最新

  • doupu5941 2012-10-03 12:48
    关注

    You can detect it by simply waiting for the request to appear on the server.

    If you get a request, the browser has not cached it.

    If you do not want the browser to cache it, simply say so in the http headers. Then you'll get the request every time.

    Edit: Ok, if you WANT caching of the image, simply send cache headers that allow for indefinite caching in the browser. Usually the image will then be requested only once. The detection of the request stays the same.

    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备