I have a site on a shared server or more accurately a reseller server which is basically shared.
I have podcasts that folks will be downloading to view. Is there anyway to utilize PHP to track the bandwidth used up from the downloads of these files?
You could think of this as hosting and I could simply let the hosting account track the bandwidth used, BUT I have multiple clients under one account and need to track the bandwidth used based on the files downloaded. I know which ones belong to which customer, but I need to be able to let the customer know the bandwidth used since their fee will depend upon this.
Thanks in advance for your time and assistance.
Greg
Tracking File Download bandwidth
Moderator: General Moderators
-
myitanalyst
- Forum Newbie
- Posts: 24
- Joined: Wed Dec 14, 2005 4:31 pm
create an intermediate page you could, dump file data into a database it would.
ex:
Main page
intermediate page
ex:
Main page
Code: Select all
echo "<a href=\"intermediate.php?fid=1\">Some Link Text</a>";Code: Select all
switch($_GET['fid'])
{
case 1:
$filename = "somefile.avi";
break;
}
// get info for $filename and add size to db
$filesize = filesize($filename);
mysql_query("update myTable set filesize = $filesize + filesize")
or die(mysql_error())With that you get the problems if a user stops the download 10% into it, you are reporting it wrong, I would say turn on implicit output buffer flushing and output 1024 bytes at a time, updating the database every time. This will definitely hit the database harder so you might want to only update it every 50kb or so, but this way when the user cancels it the client is being billed for bandwidth he/she did not infact use.
- trukfixer
- Forum Contributor
- Posts: 174
- Joined: Fri May 21, 2004 3:14 pm
- Location: Miami, Florida, USA
you could just increment a value until it output stops an dthen insert to DB, hwoever , again, you're making php work harder than it has to - I'd side with Burrito on this one..if it is a concern that a user might repeatedly start downloads and then stop them , set a check for teh same IP to instantiate additional downloads of teh same file within a set time frame (say if a user tries to down load the same file a second time in, say 2 minutes or even 30 minutes , dont even report it, dont start the download, dont update the database, and just redirect the user to a "you've already downloaded fileX" )
so I would go with the least intrusive , and fastest delivery method (imagine if you get hit with 100,000 requests a day where the processing necessary to break a file into chunks and the check if it has been sent and then update a counter and write to database, as opposed to simply determine file size and record it to DB .. on average, it's more likely the download *will* complete than to have it interrupted, so teh bandwidth usage would be accurate enough for the purpose. so the extra CPU is only going to load down teh server, potentially to teh point his hosting account gets "shut off" , or the server crashes..)
so I would go with the least intrusive , and fastest delivery method (imagine if you get hit with 100,000 requests a day where the processing necessary to break a file into chunks and the check if it has been sent and then update a counter and write to database, as opposed to simply determine file size and record it to DB .. on average, it's more likely the download *will* complete than to have it interrupted, so teh bandwidth usage would be accurate enough for the purpose. so the extra CPU is only going to load down teh server, potentially to teh point his hosting account gets "shut off" , or the server crashes..)
-
myitanalyst
- Forum Newbie
- Posts: 24
- Joined: Wed Dec 14, 2005 4:31 pm
-
myitanalyst
- Forum Newbie
- Posts: 24
- Joined: Wed Dec 14, 2005 4:31 pm
Ok... I am new to PHP... so how do I do this? This idea sounds like it may be workable, but I could make it update based on larger chunks instead of something small.jshpro2 wrote:With that you get the problems if a user stops the download 10% into it, you are reporting it wrong, I would say turn on implicit output buffer flushing and output 1024 bytes at a time, updating the database every time. This will definitely hit the database harder so you might want to only update it every 50kb or so, but this way when the user cancels it the client is being billed for bandwidth he/she did not infact use.
Right now I understand the concept, but don't know how to implement.
What does the buffering? how do I make this happen?
Thanks for the help.
if you decide to go my route, you could send the file on the same page that you're inserting the info to the db and just use header()'s to send the file.myitanalyst wrote:So after doing the above would I then redirect them to the MP3 file being requested?