php - 如何跟踪文件下载

标签 php apache logging download analytics

我有一个在 Flash 播放器中播放 mp3 的网站。如果用户单击“播放”,Flash 播放器会自动下载 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 代码,因此可以回答这个问题。

但是,如果知道如何使用其他方法(无需切换主机)来跟踪下载,那就太好了。

最佳答案

有趣的是我在两天前为我所有的音乐写了一个 php 媒体库。我有一个类似的问题。我正在使用 http://musicplayer.sourceforge.net/为玩家。播放列表是通过 php 构建的。所有音乐请求都转到一个名为 xfer.php?file=WHATEVER

的脚本
$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/en/function.base64-encode.php

如果您使用需要 mp3 文件或其他文件的实际链接的 JavaScript 或 Flash 播放器(例如 JW 播放器),您可以附加文本“&type=.mp3”,以便最终链接变为: “www.example.com/xfer.php?file=34842ffjfjxfh&type=.mp3”。这样看起来它以 mp3 扩展名结尾而不影响文件链接。

关于php - 如何跟踪文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/146/

相关文章:

php - 我在 Prestashop 中看不到我的测试模块页面

apache - Apache 2.4 中的 CGI 脚本运行但返回空响应?

python - 如何抑制来自 Twisted 的嘈杂工厂启动/停止日志消息?

java - 如何让 java 日志输出显示在一行上?

php - 无法使用 pdo 导出到 csv

php - suPHP 导致目录所有权错误

apache - 将多个域/URL 重定向到一个规范域

android - 将 IP 指向 android webview 应用程序

mysql - 使用 ado.net 记录 mysql 表中的数据更改

php - JMSSerializerBundle,反序列化后持久化没有关系