Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-31-2012, 02:19 AM   PM User | #1
TheCracker
New Coder

 
Join Date: Sep 2010
Posts: 65
Thanks: 4
Thanked 0 Times in 0 Posts
TheCracker is an unknown quantity at this point
How to effectively measure bandwidth used by a user

Hello,

I have a downloading website, where i measure the bandwidth used by every member.

For example the member is downloading a 1GB file, i want to measure how amny MB the user has downloaded in.

I'm currently using this code, it works but it creates huge load on the my server

Code:
	while (!feof($fp) && (connection_status()==0)) {
		$recv = @stream_get_line($fp, 1048577);
		@print $recv;
		@flush();
		@ob_flush();

		$query = mysql_query("UPDATE userband SET today = today + 1  WHERE username='$username'"); 
		$query = mysql_query("UPDATE userband SET month = month + 1 WHERE username='$username'"); 
		$query = mysql_query("UPDATE userband SET total = total + 1 WHERE username='$username'");

	}
is there a more efficient way to do this? i keep track of bandwidth used in month, yeat and total. as you can see for every 1MB of chunk data 3 SQL statements are executed but the download happens fast so many many sql statements are executed in one second

Advice is appreciated, thank you
TheCracker is offline   Reply With Quote
Old 12-31-2012, 03:37 AM   PM User | #2
weir-07
New Coder

 
Join Date: Sep 2007
Posts: 98
Thanks: 25
Thanked 4 Times in 4 Posts
weir-07 is an unknown quantity at this point
Might be easier if you just create a 'logs' table with fields like username:action:mb:stamp:url:uip

After each action you could insert a record populating this - just make a function so you can perform it easily.
InsertStat($username,"downloaded $filename",$mb,time(),$url,$uip);

Then make a page which takes this data and spits it out on a page for you filtered user by user for the current month.
weir-07 is offline   Reply With Quote
Old 12-31-2012, 02:30 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
For a start, you're running 3 queries there all on the same table. Why don't you just set those 3 columns to their new values in one query?

Also, you're doing it on every pass of the loop. Why not just do it at the end of the script execution? You don't need those numbers in real time do you? (and even if you did, updating 3 columns in one query is still 1/3 of the workload on the sql server).
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 12-31-2012, 06:39 PM   PM User | #4
TheCracker
New Coder

 
Join Date: Sep 2010
Posts: 65
Thanks: 4
Thanked 0 Times in 0 Posts
TheCracker is an unknown quantity at this point
You are absolutely right in this, I don't know why i have done this. I have also though to add "LIMIT 1" at the end of the sql query to avoid it going through all of the records

But i do want it in real time, but in case i don't want and the user terminates the download in middle how can i execute a php command when the downloader disconnect?

Quote:
Originally Posted by tangoforce View Post
For a start, you're running 3 queries there all on the same table. Why don't you just set those 3 columns to their new values in one query?

Also, you're doing it on every pass of the loop. Why not just do it at the end of the script execution? You don't need those numbers in real time do you? (and even if you did, updating 3 columns in one query is still 1/3 of the workload on the sql server).
TheCracker is offline   Reply With Quote
Old 12-31-2012, 07:46 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
register_shutdown_function()

That will allow you to run a function when the script ends, user disconnects etc.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
TheCracker (01-03-2013)
Old 01-03-2013, 08:34 PM   PM User | #6
TheCracker
New Coder

 
Join Date: Sep 2010
Posts: 65
Thanks: 4
Thanked 0 Times in 0 Posts
TheCracker is an unknown quantity at this point
i think that is what i'm looking for

Thank you
TheCracker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:10 AM.


Advertisement
Log in to turn off these ads.