Search code examples
phpmp3mediaaudio-streamingconnection

Monitor active server connections using PHP


I want to be able to keep a track of active connections to my server. Specifically I want to keep a track of how long users spend streaming MP3 files so I can use this information to build statistics on how long users spend listening to individual tracks etc. Since there is no interraction with PHP scripts (or at least there isn't at this point) I am wondering if there is a way of polling the server to find a list of currently connected IPs? Perhaps an alternative would be to handle the streaming via a PHP script using readfile? any ideas? Thanks :)


Solution

  • The only way to do this effectively is to stream the file via your PHP script with readfile(). Using this method, you can count the number of bytes sent and make a guess about how much was played client-side.

    It is impossible to know however (without something measuring client-side) what was played. Different players buffer data differently, and even that is often variable on network conditions. You can only know how much data you sent to the client. Usually for analytics, this is enough information, as it is typically used in a comparative manner against other tracks being played. You can use a tool, such as FFMPEG, to determine the audio length of the file, and divide that by the percentage of data in the file you sent. This will be less-accurate for variable bitrate tracks, but will be the most efficient way to get you in the ballpark.