跟踪(直接)文件下载的最佳方式

Adr*_*ian 3 html php apache statistics .htaccess

跟踪直接文件下载的最佳方法是什么?我找到了一些解决方案,例如:

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

但它对我不起作用,当我试图下载文件时,我只得到一个空白页面+我不知道它是否足够安全......

Google Analytics仅适用于javascript,无法跟踪直接文件下载.

Best是一个安全且自己的托管解决方案.

Adr*_*ian 10

随意使用:)

的.htaccess:

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

MySQL的:

CREATE TABLE `download` (
    `filename` varchar(255) NOT NULL,
    `stats` int(11) NOT NULL,
    PRIMARY KEY  (`filename`)
)
Run Code Online (Sandbox Code Playgroud)

的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;
}

?>
Run Code Online (Sandbox Code Playgroud)

来源 - gayadesign.com


Jem*_*lus 6

您的 apache 日志应该包含很多信息,但我认为您要求的是更多地控制记录的内容和时间。所以你想要做的是有两个页面:一个包含文件链接,另一个跟踪文件,如下所示:

file_page.php

<a href="download.php?id=1234">Download File!</a>
Run Code Online (Sandbox Code Playgroud)

下载.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);
Run Code Online (Sandbox Code Playgroud)

反正就是这样。

完成后页面将为空白,但所有浏览器都应在页面加载时开始下载文件。

所以我在这里做的是保存文件 ID、当前日期时间和下载它的人的用户 ID(来自 $_SESSION 变量)。您可能想要存储更多信息,例如用户的 IP 地址、HTTP_REFERRER 或其他 $_SERVER 信息,以便您可以跟踪用户来自何处以及他们下载的时间和内容。

祝你好运。