开发者

Counting how many times a image is being served from server?

I want to count the no. of times a image is being serve开发者_如何学God 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.


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


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.


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.


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜