====== ダウンロード数をカウントする ======
ダウンロード数をカウントします。
* 連続した同じIPアドレスはカウントしません。
* データベースを使うので、ファイルを作成しません。
* データベースの設定は、ユーザー認証のMySQLの設定を使うので、設定済みであれば設定不要。
- データベースにテーブルを作成しておく。
CREATE TABLE `dlcount` (
`id` VARCHAR( 255 ) NOT NULL DEFAULT '',
`count` INT( 15 ) UNSIGNED NOT NULL DEFAULT '0',
`ip` VARCHAR( 15 ) DEFAULT NULL ,
PRIMARY KEY ( `id` )
)
- lib/exe/fetch.phpを開き、sendFileの前にDownload_countを挿入、後ろにfunction Download_countを定義する。
// finally send the file to the client
Download_count($MEDIA);
sendFile($FILE,$MIME,$CACHE);
/* ------------------------------------------------------------------------ */
function Download_count($ID) {
global $conf;
$dlc_server=$conf['auth']['mysql']['server'];
$dlc_user=$conf['auth']['mysql']['user'];
$dlc_password=$conf['auth']['mysql']['password'];
$dlc_database=$conf['auth']['mysql']['database'];
$dlc_table="dlcount";
$dlc_ip = $_SERVER['REMOTE_ADDR'];
$con = @mysql_connect ($dlc_server, $dlc_user, $dlc_password);
if ($con) {
$res=mysql_select_db($dlc_database, $con);
if ($res) {
$query = "SELECT count,ip FROM $dlc_table WHERE id = '$ID'";
$res = @mysql_query($query, $con);
$row = @mysql_fetch_object($res);
$count = intval($row->count);
if (@mysql_num_rows($res) == 0) {
$query = "INSERT INTO $dlc_table VALUES ('$ID','1', '$dlc_ip')";
$res2 = @mysql_query($query, $con);
$count = 1;
} else {
if ($dlc_ip != $row->ip) {
$count++;
$query = "UPDATE $dlc_table SET ip='$dlc_ip', count='$count' WHERE id = '$ID'";
$res2 = @mysql_query($query, $con);
}
}
}
}
}