views:

58

answers:

2

Hello. I am working on a community photo sharing site, and I need some way of controlling the bandwidth. For example I would like to set a maximum bandwidth allowed, which can be image size x the number of times the image was viewed. Similar to what a site like photobucket does (when they replace an image with a bandwidth exceeded image).

I am wondering what the best solution would be. How can I track the number of times an image is viewed? Maybe I need to use something like Google Analytics and then run a cron job every so often to see if any image has exceeded the bandwidth limit? I think this may be overkill and I am over thinking this problem.

By the way, I am developing in PHP.

Any ideas?

Thanks!

UPDATE

Ok, so it seems mod-rewrite is the way to go. Right now I have the following code in .htaccess:

RewriteEngine on
RewriteRule .*.(gif|jpg|jpeg|png)$ /include/image.php

When I go to an actual image (i.e. http://...picture.jpeg), the PHP page image.php loads, but when an image is in an img tag (i.e. img src=http://...picture.jpeg ), the image does not load (good), but the php file does not load in its place.

How do I fix it?

Thanks

SOLUTION

If anyone is interested, this worked (although it can probably be improved):

RewriteEngine on
RewriteRule (.*).(jpg|jpeg|gif|png|wbmp)$ /include/image.php?location=$1&format=$2 [NC,L]

If you look at the $_GET variables image.php is receiving, you can probably figure out what to do from there.

+3  A: 

I assume you are keeping a database of images, and details. Could you not store requests to images in your database? If the number of requests for a certain day exceed n, you refuse the image.

Going this route would mean you would have to serve all images through a PHP script, as opposed to letting people have direct access to the image itself. Using mod_rewrite with apache, you could rewrite all requests for images/001830.jpg to images/serve.php?id=001830 which would allow you to perform checks against the stored data to determine if you output the real image, or a generic "bandwidth exceeded" image in its place.

Jonathan Sampson
Right, but how do I know if an image was requested?
Domenic
Serve all images through a PHP script.
Jonathan Sampson
Thats the simplest solution but it can mess a bit in performance when traffic became bigger. Anyway its a good idea for start (read about mod_rewrite - http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html)
tomaszsobczak
Could you write an example of this mod_rewrite, I'm having some trouble with it..
Domenic
Domenic, you should start a second question. This is an entirely different topic now :) Please consider accepting my answer, and be sure to let us know the URL of the *new* question.
Jonathan Sampson
A: 

Keep a counter in your database and add 1 to it every time a user looks at an image, you can also sort the date and time, if the last view was a prior day reset the counter to 1.

Hogan