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.