apache 如何跟踪文件下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:41:18  来源:igfitidea点击:

How do I track file downloads

phpapacheloggingdownloadanalytics

提问by Grant

I have a website that plays mp3s in a flash player. If a user clicks 'play' the flash player automatically downloads an mp3 and starts playing it.

我有一个在 Flash 播放器中播放 mp3 的网站。如果用户单击“播放”,则 Flash 播放器会自动下载 mp3 并开始播放。

Is there an easy way to track how many times a particular song clip (or any binary file) has been downloaded?

是否有一种简单的方法可以跟踪特定歌曲剪辑(或任何二进制文件)的下载次数?



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.

播放链接是指向实际 mp3 文件的链接还是指向弹出播放器的某些 javascript 代码的链接?

如果是后者,您可以轻松地在其中添加自己的日志记录代码来跟踪点击次数。

如果是前者,您将需要可以跟踪 Web 服务器日志本身并进行区分的东西。我的托管计划带有 Webalizer,它可以很好地做到这一点。

It's a javascript code so that answers that.

这是一个 javascript 代码,因此可以回答这个问题。

However, it would be nice to know how to track downloads using the other method (without switching hosts).

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

采纳答案by w-ll

The funny thing is I wrote a php media gallery for all my music 2 days ago. I had a similar problem. I'm using http://musicplayer.sourceforge.net/for the player. And the playlist are built via php. All music request go there a script called xfer.php?file=WHATEVER

有趣的是,我两天前为我所有的音乐写了一个 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, '-_,', '+/='));
}

And when you call files use something like:

当您调用文件时,请使用以下内容:

function base64_url_encode($input) {
     return strtr(base64_encode($input), '+/=', '-_,');
}

http://us.php.net/manual/en/function.base64-encode.php

http://us.php.net/manual/en/function.base64-encode.php

If you are using some javascript or a flash player (JW player for example) that requires the actual link to be an mp3 file or whatever, you can append the text "&type=.mp3" so the final linke becomes something like: "www.example.com/xfer.php?file=34842ffjfjxfh&type=.mp3". That way it looks like it ends with an mp3 extension without affecting the file link.

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

回答by Tim Boland

Use your httpd log files. Install http://awstats.sourceforge.net/

使用您的 httpd 日志文件。安装http://awstats.sourceforge.net/

回答by randomx

Use bash:

使用 bash:

grep mp3 /var/log/httpd/access_log | wc

回答by Vinay Y S

If your song / binary file was served by apache, you can easily grep the access_log to find out the number of downloads. A simple post-logrotate script can grep the logs and maintain your count statistics in a db. This has the performance advantage by not being in your live request code path. Doing non-critical things like stats offline is a good idea to scale your website to large number of users.

如果您的歌曲/二进制文件是由 apache 提供的,您可以轻松地 grep access_log 以找出下载次数。一个简单的 post-logrotate 脚本可以 grep 日志并在数据库中维护您的计数统计信息。由于不在您的实时请求代码路径中,因此具有性能优势。离线进行统计等非关键操作是将您的网站扩展到大量用户的好主意。

回答by saint_groceon

You could even set up an Apache .htaccess directive that converts *.mp3 requests into the querystring dubayou is working with. It might be an elegant way to keep the direct request and still be able to slipstream log function into the response.

您甚至可以设置一个 Apache .htaccess 指令,将 *.mp3 请求转换为 dubayou 正在使用的查询字符串。这可能是一种保持直接请求并仍然能够将日志功能整合到响应中的优雅方式。

回答by Dillie-O

Is the play link a link to the actual mp3 file or to some javascript code that pops up a player?

播放链接是指向实际 mp3 文件的链接还是指向弹出播放器的某些 javascript 代码的链接?

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.

如果是前者,您将需要可以跟踪 Web 服务器日志本身并进行区分的东西。我的托管计划带有 webalizer,它可以很好地做到这一点。

回答by icc97

The problem I had with things like AWStats / reading through web server logs is that large downloads can often be split in data chunks within the logs. This makes reconciling the exact number of downloads quite hard.

我在使用 AWStats / 读取 Web 服务器日志之类的问题时遇到的问题是,大量下载通常可以拆分为日志中的数据块。这使得协调下载的确切数量变得非常困难。

I'd suggest the Google Analytics Event Tracking, as this will register once per click on a download link.

我建议使用 Google Analytics Event Tracking,因为每次点击下载链接都会注册一次。

回答by Mike H

Is there a database for your music library? If there is any server code that runs when downloading the mp3 then you can add extra code there to increment the play count. You could also have javascript make a second request to increment the play count, but this could lead to people/robots falsely incrementing counts.

你的音乐库有数据库吗?如果在下载 mp3 时有任何服务器代码运行,那么您可以在那里添加额外的代码以增加播放次数。您还可以让 javascript 再次请求增加播放次数,但这可能会导致人员/机器人错误地增加播放次数。

I used to work for an internet-radio site and we used separate tables to track the time every song was played. Our streams were powered by a perl script running icecast, so we triggered a database request every time a new track started playing. Then to compute the play count we would run a query to count how many times a song's id was in the play log.

我曾经在一个互联网广播网站工作,我们使用单独的表格来跟踪每首歌曲的播放时间。我们的流由运行 icecast 的 perl 脚本提供支持,因此每次开始播放新曲目时我们都会触发数据库请求。然后为了计算播放次数,我们将运行一个查询来计算歌曲的 id 在播放日志中出现的次数。