2011-11-16 6 views
1

직접 파일 다운로드를 추적하는 가장 좋은 방법은 무엇입니까?파일 다운로드를 추적하는 가장 좋은 방법은

http://www.gayadesign.com/diy/download-counter-in-php-using-htaccess/

하지만 나를 위해 작동하지 않습니다, 나는 단지 내가 파일을 다운로드하려고 빈 페이지를 얻을 + 내가 충분히 알고하지 않습니다 :이 하나의 예를 들어, 어떤 해결책을 발견 보안 여부 ...

Google 웹 로그 분석은 자바 스크립트에서만 작동하며 직접 파일 다운로드를 추적 할 수 없습니다.

최상의 보안 및 자체 호스팅 솔루션이 될 것입니다.

+0

어떤 프로토콜이 끝났습니까? 당신의 통나무는 당신이 알 필요가있는 모든 것을 알려주지 않을 것입니까? – Treborbob

+1

로그 분석기를 가져 와서 access_log에서 직접 다운로드 URL을 꺼내십시오. –

+0

Apache 로그는 사용자에게 친숙하지 않으며 나중에 카운터에 카운터를 표시하려고합니다. – Adrian

답변

5

자유롭게 사용 :

htaccess로 :

RewriteEngine on  
RewriteRule ^(.*).(rar|zip|pdf)$ http://xy.com/downloads/download.php?file=$1.$2 [R,L]  

MySQL의 :

CREATE TABLE `download` (
    `filename` varchar(255) NOT NULL, 
    `stats` int(11) NOT NULL, 
    PRIMARY KEY (`filename`) 
) 

download.php

<?php 

mysql_connect("localhost", "name", "password") 
or die ("Sorry, can't connect to database."); 
mysql_select_db("dbname"); 
$baseDir = "/home/public_html/downloads"; 
$path = realpath($baseDir . "/" . basename($_GET['file'])); 

if (dirname($path) == $baseDir) { 
if(!is_bot()) 
mysql_query("INSERT INTO download SET filename='".mysql_real_escape_string(basename($_GET['file']))."' ON DUPLICATE KEY UPDATE stats=stats+1"); 


header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=" . basename($_GET['file'])); 
header("Content-Length: ".filesize($path)); 
header("Content-Type: application/force-download"); 
header("Content-Transfer-Encoding: binary"); 
ob_clean(); 
ob_end_flush(); 
readfile($path);  
} 

function is_bot() 
{ 

    $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi", 
    "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", 
    "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", 
    "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", 
    "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", 
    "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", 
    "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot", 
    "Butterfly","Twitturls","Me.dium","Twiceler"); 

    foreach($botlist as $bot) 
    { 
     if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false) 
     return true; // Is a bot 
    } 

    return false; 
} 

?> 

출처 - gayadesign.com

+0

최신 버전의 MySQL은 아마도 [mysqli_connect] (http://php.net/manual/en/function.mysqli-connect.php)와 [mysqli_real_escape_string] (http : // php.net/manual/ko/mysqli.real-escape-string.php) 대신에 – mikeDOTexe

3

귀하의 아파치 로그에는 많은 정보가 포함되어 있어야하지만, 귀하가 요구하는 것은 기록되는 내용과 때를 제어하는 ​​것입니다. 그래서 당신이 무엇을 원하는 두 페이지가 있습니다 : 하나를 파일에 대한 링크와 함께, 그래서 같이 파일을 추적하는 다른 :

file_page.php

<a href="download.php?id=1234">Download File!</a> 

download.php

<? // Code to track the file using PHP, whether that means storing data in a database, saving to a log, or emailing you. I'd use a DB, like so: 

    // Prep the vars 
    $file_id = $_GET['file_id']; // You should sanitize this first. 
    $file_path = '/files/'.$file_id.'.pdf'; 

    // Save data to database 
    mysql_query('INSERT INTO download_log 
     SET file_id = '.$file_id.', 
      date_downloaded = '.date('Y-m-d H:i:s').', 
      user_id = '.$_SESSION['user_id']); 

    // Now find the file and download it 
    header('Content-type: application/pdf'); 
    header('Content-Disposition: attachment; filename='.$file_id.'.pdf); // or whatever the file name is 
    readfile($file_path); 

어쨌든.

완료되면 페이지가 비어 있지만 페이지가로드 될 때 모든 브라우저에서 파일 다운로드를 시작해야합니다.

그래서 여기서 내가하고있는 일은 파일 ID, 현재 날짜 시간 및 $ _SESSION 변수에서 다운로드 한 사람의 사용자 ID를 저장하는 것입니다. 사용자의 IP 주소, HTTP_REFERRER 또는 기타 $ _SERVER 정보와 같은 더 많은 정보를 저장하여 사용자의 출신 위치와 다운로드시기를 추적 할 수 있습니다.

행운을 빈다.

+0

이렇게하면됩니다. 그러나,'readfile'을 통해 전송하는 대용량 파일이 있고 바쁜 사이트가 있다면, 이것은 많은 양의 RAM을 소비하게된다는 것을 명심하십시오. –

+0

@MichaelIrey, RAM 소비가 적은 파일 다운로드를 추적하는 다른 방법이 있습니까? – ezpresso

관련 문제