跟踪(直接)文件下载的最佳方式
跟踪直接文件下载的最佳方法是什么?我找到了一些解决方案,比如这一个:跟踪(直接)文件下载的最佳方式
http://www.gayadesign.com/diy/download-counter-in-php-using-htaccess/
但它不是为我工作,我只得到当我试图下载一个文件一个空白页+我不知道这是不够安全与否...
谷歌Analytics只适用于JavaScript,无法跟踪直接下载文件。
最好将是一个安全和自己的托管解决方案。
随意使用:)
的.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
使用较新版本的MySQL可能会使用[mysqli_connect](http://php.net/manual/en/function.mysqli-connect.php)和[mysqli_real_escape_string](http:// php。net/manual/en/mysqli.real-escape-string.php) – mikeDOTexe
你的apache日志应该包含很多信息,但我认为你所要求的是更多地控制什么被记录以及什么时候被记录。所以,你想要做的是有两个页面:一个链接到文件,跟踪文件,像这样对方:
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,当前日期时间和用户ID(从$ _SESSION变量)。您可能希望存储更多信息,例如用户的IP地址,HTTP_REFERRER或其他$ _SERVER信息,以便您可以跟踪用户来自何处以及何时何地下载。
祝你好运。
这将工作。但是,请记住,如果您通过'readfile'发送大文件并且网站很繁忙,则会消耗大量内存。 –
@MichaelIrey,是否有任何其他方法跟踪文件下载与低RAM消费? – ezpresso
这是什么协议?不要你的日志告诉你你需要知道的一切吗? – Treborbob
获取日志分析器并从access_log中提取直接下载URL? –
Apache日志不是非常用户友好,我想稍后在我的网站上显示计数器。 – Adrian