English 中文(简体)
Controlling image bandwidth issues on a community site
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签