views:

108

answers:

5

I'm trying to find a way of finding out who is downloading what image from an image gallery. Users can download using a button beside the thumbnail or right click and use the "save link as" Is it possible to relate a user session or ID to a "save link as" action from all browsers using either PHP or JavaScript.

A: 

You need a gateway script, like ImageDownload.php?picture=me.jpg, or something like that.

That page whould return the image bytes, as well as logging that the image is downloaded.

Jesper Blad Jensen aka. Deldy
A: 

Because the images being saved are on their computer locally there would be no way to get that kind of information as they have already retrieved the image from your system. Even with javascript the best I know that you could do is to log each time a user presses the second mousebutton using some kind of ajax'y stuff.

I don't really like the idea, but if you wanted to log everytime someone downloaded an image you could host the images inside a flash or java app that made it a requirement to click a download image button. That way the only way for them to get the image without doing that would be to either capture packets as they came into their side or take a screenshot.

PintSizedCat
+1  A: 

Yes, my preferred way of doing this would be via PHP. You'd have to set up a script which would load up the file and send it to the user browser. This script would also be able to log the download somewhere (e.g. your database).

For example - in very rough pseudo-code:

download.php

$file = $_GET['file'];
updateFileCount($file);
header('Content-Type: image/jpeg');
sendFile($file);

Then, you just have your download link point to download.php instead of the actual file. (Note that updateFileCount and sendFile are functions that you would have to provide, of course - this script is an example of a download script which you could use)

Note: I highly recommend avoiding the use of $_GET['file'] to get the whole filename - malicious users could use it to retrieve sensitive files from your web server. But the safe use of PHP downloads is a topic for another question.

Phill Sacre
A: 

Your server access logs should already have the request for the non-thumbnailed version of the file, so you just need to modify the log format to include the sessionid, which I presume you can map back to a user.

Hank Gay
A: 

I agree strongly with the suggestion put forward by Phill Sacre. For what you are looking for this is the way to go.

It also has the benefit of being potentially able to keep the tracked files out of the direct web path so that they can't be direct linked to.

I use this method in a client site where the images are paid content so must be restricted access.

Laith