tags:

views:

58

answers:

5

I want to count the no. of times a image is being served from our server. I have some images in a website and want to count the no. when these images are showed on web pages(served from the server to my website and if hotlinked). Is there any way to accomplish this. I know php so if there is some way doing it in php it would be really helpful.

advice please

thank you.

+1  A: 

If you're wanting something beyond parsing server-logs, you'd have to setup a database to manage the list of images, and the number of times they're accessed. Serve the images through a .php script which increments the db value with each request. You could use a flat-file system too, but I prefer the db-solution.

You wouldn't need to worry about the source of your image if you implement .htaccess and apache's mod_rewrite. You could serve url's like this:

http://mysite.com/images/001.jpg

Which would be understood on the server as:

http://mysite.com/images.php?id=001

Thus providing a basis to perform database-actions, and scripted logic.

Jonathan Sampson
If i serve image through a php script then my url to image will look something like this http://www.mysite.com/servimage.php?image=someimage. I guess the image will not be displayed if i use this link in webpages. It will work fine if i directly click this link.Correct me if i am wrong
RishiPatel
@RishiPatel, using .htaccess you could have urls like this: mysite.com/images/001.jpg which would be interpreted as mysite.com/images.php?id=001 by your server.
Jonathan Sampson
Thanks.. seems it wud work.
RishiPatel
Jonathan's spot on... If I'm hosting an image/file/song that I want tracked, I'll serve it through a php script that will let me log whatever details I need (IP, timestamp, any user info if I've got user accounts, etc...)
AlishahNovin
+6  A: 

Can't you look at your server logs for that?

sprugman
+1 for simplicity.
Jonathan Sampson
Yep, the logs are the place to look if they're available. Why reinvent the wheel? And if you don't want to parse them yourself (not that grep is hard to use), Webalyzer is your friend.
Dathan
A: 

You should be able to gather this information using the log files and an analytics package. If you are running IIS a really good one to look into (and free for 1 domain) is SmarterTools' SmarterStats. www.smartertools.com

Jeremy H
A: 

The answers recommending looking in the logs are right. But if for some reason that's not acceptable, it's not hard to configure a php script to handle this.

1) Create a rewrite rule (using mod_rewrite) to transparently rewrite requests to your image to go to a php file instead, with the image's name as a parameter. 2) your php script can log the request, then send out the appropriate MIME type for the image, and dump the real file to the output buffer (this shouldn't be affected by your rewrite rule as long as you load from the file system rather than using a URL stream).

Dathan
@Dathan dint understand the 'the file system rather than using a URL stream'
RishiPatel
+1  A: 

You can use Microsoft's LogParser to query your server logs using a query something like this:

c:\Program Files\Log Parser 2.2> logparser "select cs-uri-stem, count(*) as Hits from C:\Your\Log\File\Path\ex091002.log where cs-uri-stem like 'imagefilename.jpg' or where cs-uri-stem like 'anotherimage.jpg' group by cs-uri-stem order by Hits DESC" -i:w3c

You can even have it output to a text file or a graph (requires Excel, I believe) if you need something to display on a page. You'll probably have to change the query if you're using Apache logs, not sure.

Dave Cowart