89

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.

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.

It's a javascript code so that answers that.

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

Rahul
  • 18,271
  • 7
  • 41
  • 60
Grant
  • 11,799
  • 13
  • 42
  • 47

8 Answers8

43

The funny thing is I wrote a php media gallery for all my musics 2 days ago. I had a similar problem. I'm using http://musicplayer.sourceforge.net/ for the player. And the playlist is built via php. All music requests go to a script called 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

If you are using some JavaScript or a flash player (JW player for example) that requires the actual link of an mp3 file or whatever, you can append the text "&type=.mp3" so the final link 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.

Tanjim Ahmed Khan
  • 650
  • 1
  • 9
  • 21
w-ll
  • 3,895
  • 2
  • 31
  • 29
  • 20
    there is a directory traversal vulnerability in this script! An attacker can pass in xfer.php?file=../../../passwd or whatever else they want! Be careful!!! – Alex Weinstein Sep 22 '08 at 04:36
  • 7
    this will blow up the server's memory limits if the files are too big and your traffic is high.. something i've experienced myself. – tmsimont Jul 08 '11 at 17:17
  • 1
    how to fix the "directory traversal vulnerability" ? – 456543646346 Jun 18 '13 at 00:49
  • 1
    @anarchOi: The easiest way would be to compare the GET parameter *(`$_REQUEST['file']`)* against a whitelist of known-good file names. For instance, a listing of all files in the directory you store your files. Make sure you **only** use that directory for storing files you want to be downloadable... – BlueRaja - Danny Pflughoeft Nov 14 '13 at 02:34
  • 1
    @tmsimont, Have you found out any ways of lowering memory consumption? – ezpresso Aug 21 '17 at 22:55
  • @ezpresso ya check out the links I put up on on this question: https://stackoverflow.com/questions/6627952/why-does-readfile-exhaust-php-memory (also kind of funny that i'd reply to a reply to something i posted 6 years ago haha – tmsimont Aug 22 '17 at 00:34
30

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

Tim Boland
  • 10,127
  • 8
  • 27
  • 25
26

Use bash:

grep mp3 /var/log/httpd/access_log | wc
randomx
  • 2,357
  • 1
  • 21
  • 30
  • there are at least 2 flaws in this method: it counts GET and HEAD requests and counts all HTTP response codes. For example, there may be a lot of 206 responses which will lead you to overestimate downloads. – 8ctopus Dec 04 '20 at 06:29
14

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.

Vinay Y S
  • 268
  • 2
  • 4
13

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.

saint_groceon
  • 6,167
  • 5
  • 32
  • 26
7

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.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
4

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.

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

icc97
  • 11,395
  • 8
  • 76
  • 90
4

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.

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.

Mike H
  • 369
  • 2
  • 11