Bandwidth Limit User

Posted by user45611 on Server Fault See other posts from Server Fault or by user45611
Published on 2010-06-18T14:52:31Z Indexed on 2010/06/18 14:54 UTC
Read the original article Hit count: 428

Hello, i'm saxtor i would like to know how to limit users bandwidth for 10gb per day however i dont want to limit them by ipaddress because if they where to go to an internet cafe the users at the cafe will be restricted with that quota, i need to log them via sockets, example the user request to download a file from http://localhost with there username and password, when they download the file sql will update there bandwidth they used, i have a script here but its not working my buffer doesnt work that rate when a user uses multiple connections thanks for the help!.

/** * @author saxtor if you can improve this code email me @saxtorinc.com * @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) { //pass client Range header to rapidshare // _insert($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");

//awful hack to pass rapidshare the premium cookie $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;

?>

© Server Fault or respective owner

Related posts about php

Related posts about bandwidth