Monitor ip bandwidth transfer via php - how to make code?
Posted: Sat Oct 20, 2007 1:13 pm
After a considerable amount of googling, I basically was left with these two scripts, with some (but not much) of an idea on how to integrate them.
The first one:
This one seems to do what I want, but I don't know how to add to it to store the bandwidth usage (based on ip).
This one, however, has the potential to allow the first script above to be integrated with it; but it logs the number of times a given file is downloaded, not total bandwidth:
It shouldn't matter how many times I download a single file or an assortment of files, the script should just log the ip of the user and the amount of bandwidth they have transferred (into a MySQL database). I should be able to configure (via a variable or whatever) how much data should be allowed transferred until the script blocks off the ip for a given number of time (again configurable by a variable, e.g. $time_until_reset=3600, etc.).
So I would like if somebody could change whatever is necessary to do the following: when a user reuests a file, their ip is logged and the total amount of data they download is logged. When they have reached a critical amount, their ip's further requests are blocked until a given time has elapsed.
Any comments would be useful, and please don't hesitate to ask me if anything is unclear.

The first one:
This one seems to do what I want, but I don't know how to add to it to store the bandwidth usage (based on ip).
Code: Select all
<?php
function CalcBandwidth($buffer)
{
$file_size = strlen($buffer);
//store the file size in DB or file
return ($buffer);
}
ob_start('CalcBandwidth');
?>Code: Select all
<?php
// change this value below
$cs_conn = mysql_connect('localhost', 'db_user', 'password');
mysql_select_db('db_name', $cs_conn);
mysql_query("CREATE TABLE IF NOT EXISTS `bandwidth` (
`filepath` varchar(255) NOT NULL,
`ipadres` varchar(15) NOT NULL,
`size` datetime NOT NULL,
UNIQUE KEY `filepath` (`filepath`,`ipadres`),
KEY `ipadres` (`ipadres`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
$path = addslashes($_SERVER['REQUEST_URI']);
$ip = addslashes($_SERVER['REMOTE_ADDR']);
$dl = false;
$sql = sprintf("SELECT UNIX_TIMESTAMP(last_access) last_time FROM downloaded WHERE filepath = '%s' AND ipadres = '%s' ORDER BY last_access DESC", $path, $ip);
$res = mysql_query($sql);
if (mysql_num_rows($res) > 0) {
$last_xs = mysql_result($res, 0, 'last_time')+3600;
if ($last_xs < time()) {
mysql_query(sprintf("REPLACE downloaded SET filepath = '%s', ipadres = '%s', last_access = NOW()", $path, $ip));
$dl = true;
}
} else {
$sql = sprintf("REPLACE downloaded SET filepath = '%s', ipadres = '%s', last_access = NOW()", $path, $ip);
mysql_query($sql);
$dl = true;
}
if ($dl) {
$fullPath = $_SERVER['DOCUMENT_ROOT'].$path;
if ($fd = fopen ($fullPath, "r")) {
$fname = basename($fullPath);
header('Content-type: application/octet-stream');
header('Content-Disposition: filename="'.$fname.'"');
header('Content-length: '.filesize($fullPath));
header('Cache-control: private');
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose ($fd);
exit;
}
} else {
header('HTTP/1.0 503 Service Unavailable');
die('Abort, you reached your download limit for this file.');
}
?>So I would like if somebody could change whatever is necessary to do the following: when a user reuests a file, their ip is logged and the total amount of data they download is logged. When they have reached a critical amount, their ip's further requests are blocked until a given time has elapsed.
Any comments would be useful, and please don't hesitate to ask me if anything is unclear.