如何跟踪文件下载
我有一个网站,可以在Flash Player中播放mp3. 如果用户单击"播放",则Flash Player会自动下载mp3并开始播放。
有没有一种简便的方法来跟踪特定歌曲剪辑(或者任何二进制文件)已被下载多少次?
Is the play link a link to the actual mp3 file or to some javascript code that pops up a player? If the latter, you can easily add your own logging code in there to track the number of hits to it. If the former, you'll need something that can track the web server log itself and make that distinction. My hosting plan comes with webalizer, which does this nicely.
它是javascript代码,因此可以回答。
但是,很高兴知道如何使用其他方法(无需切换主机)来跟踪下载。
解决方案:
播放链接是指向实际mp3文件的链接,还是指向弹出播放器的某些JavaScript代码的链接?
如果是后者,则可以轻松地在其中添加自己的日志记录代码,以跟踪其命中次数。
如果是前者,则需要一些可以跟踪Web服务器日志本身并进行区分的东西。我的托管计划随附webalizer,可以很好地完成此任务。
有趣的是2天前,我为所有音乐写了一个php媒体库。我有一个类似的问题。我正在使用http://musicplayer.sourceforge.net/作为播放器。播放列表是通过php构建的。所有音乐请求都转到一个名为xfer.php?file = WHEREVER的脚本
$filename = base64_url_decode($_REQUEST['file']); header("Cache-Control: public"); header('Content-disposition: attachment; filename='.basename($filename)); header("Content-Transfer-Encoding: binary"); header('Content-Length: '. filesize($filename)); // Put either file counting code here. either a db or static files // readfile($filename); //and spit the user the file function base64_url_decode($input) { return base64_decode(strtr($input, '-_,', '+/=')); }
当我们调用文件时,请使用类似以下内容的文件:
function base64_url_encode($input) { return strtr(base64_encode($input), '+/=', '-_,'); }
http://us.php.net/manual/zh/function.base64-encode.php
如果我们使用的某些JavaScript或者Flash Player(例如JW Player)要求实际的链接为mp3文件或者其他文件,则可以添加文本"&type = .mp3",这样最终的链接将变为:
" www.example.com/xfer.php?file=34842ffjfjxfh&type=.mp3"。这样,它看起来像以mp3扩展名结尾而不会影响文件链接。
我们甚至可以设置一个Apache .htaccess指令,将* .mp3请求转换为我们正在使用的dubayou查询字符串。这可能是保留直接请求并且仍然能够将日志功能整合到响应中的一种好方法。
使用httpd日志文件。安装http://awstats.sourceforge.net/
音乐库有数据库吗?如果下载mp3时有任何服务器代码在运行,则可以在其中添加额外的代码以增加播放次数。我们可能还让javascript再次请求增加播放次数,但这可能导致人员/机器人错误地增加播放次数。
我曾经在一个互联网广播站点工作,我们使用单独的表格来跟踪每首歌曲的播放时间。我们的流由运行icecast的perl脚本提供动力,因此,每当新曲目开始播放时,我们都会触发数据库请求。然后,要计算播放次数,我们将运行查询以计算歌曲ID在播放日志中的次数。
如果歌曲/二进制文件是由apache提供的,则可以轻松grep access_log找出下载次数。一个简单的logrotate后脚本可以对日志进行grep,并在db中维护计数统计信息。
由于不在实时请求代码路径中,因此具有性能优势。离线进行统计等非关键性操作是将网站扩展到大量用户的一个好主意。
使用bash:
grep mp3 /var/log/httpd/access_log | wc