Hey How are you doing guys, i am trying to limit bandwidth per users not by ipaddress for some reason my code doesnt work i need some help, what i am trying to do is to limit the download of the user that they would only have 10Gb per day to download however it seems to me that my buffer is not working when i use multiple connections it doesnt work, but when i use one connect it works 80% here is my code can you debug the error for me thanks.
/**
* @author saxtor if you can improve this code email me
[email protected]
* @copyright 2010
*/
/**
* CREATE TABLE IF NOT EXISTS `max_traffic` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`limit` int(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
*/
//SQL Connection [this is hackable for testing]
date_default_timezone_set("America/Guyana");
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Quota") or die(mysql_error());
function quota($id)
{
$result = mysql_query("SELECT `limit` FROM max_traffic WHERE id='$id' ") or
die(error_log(mysql_error()));;
$row = mysql_fetch_array($result);
return $row[0];
}
function update_quota($id,$value)
{
$result = mysql_query("UPDATE `max_traffic` SET `limit`='$value' WHERE
id='$id'")
or die(mysql_error());
return $value;
}
if ( quota(1) != 0)
$limit = quota(1);
else
$limit = 0;
$multipart = false;
//was a part of the file requested? (partial download)
$range = $_SERVER["HTTP_RANGE"];
if ($range)
{
$cookie .= "\r\nRange: $range";
$multipart = true;
header("X-UR-RANGE-Range: $range");
}
$url = 'http://127.0.0.1/puppy.iso';
$filename = basename($url);
//octet-stream + attachment => client always stores file
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
//always included so clients know this script supports resuming
header("Accept-Ranges: bytes");
$user_agent = ini_get("user_agent");
ini_set("user_agent", $user_agent . "\r\nCookie: enc=$cookie");
$httphandle = fopen($url, "r");
$headers = stream_get_meta_data($httphandle);
$size = $headers["wrapper_data"][6];
$sizer = explode(' ',$size);
$size = $sizer[1];
//let's check the return header of rapidshare for range / length indicators
//we'll just pass these to the client
foreach ($headers["wrapper_data"] as $header)
{
$header = trim($header);
if (substr(strtolower($header), 0, strlen("content-range")) ==
"content-range")
{
// _insert($range);
header($header);
header("X-RS-RANGE-" . $header);
$multipart = true; //content-range indicates partial download
} elseif (substr(strtolower($header), 0, strlen("Content-Length")) ==
"content-length")
{
// _insert($range);
header($header);
header("X-RS-CL-" . $header);
}
}
if ($multipart) header('HTTP/1.1 206 Partial Content');
flush();
$speed = 4128;
$packet = 1; //this is private dont
touch.
$bufsize = 128; //this is private dont touch/
$bandwidth = 0; //this is private dont
touch.
while (!(connection_aborted() || connection_status() == 1) && $size > 0)
{
while (!feof($httphandle) && $size > 0)
{
if ($limit <= 0 )
$size = 0;
if ( $size < $bufsize && $size != 0 && $limit != 0)
{
echo fread($httphandle,$size);
$bandwidth += $size;
}
else
{
if( $limit != 0)
echo fread($httphandle,$bufsize);
$bandwidth += $bufsize;
}
$size -= $bufsize;
$limit -= $bufsize;
flush();
if ($speed > 0 && ($bandwidth > $speed*$packet*103))
{
usleep(100000);
$packet++;
//update_quota(1,$limit);
}
error_log(update_quota(1,$limit));
$limit = quota(1);
//if( $size <= 0 )
// exit;
}
fclose($httphandle);
}
exit;