You should use a handler to be able to hook into each and every request.
This can be done quite easily, but you should then also serve the files from your php.
Update
The following code snippets assume the following:
- you have a typical "LAMP stack" installed on the hosting server
- your Apache config "allows directory overrides" (with
.htaccess
files)
- your Apache setup has
mod_rewrite
installed and enabled
- Apache+PHP has read permission of all the files created in your
docroot
- your PHP version is at least 5.5 (or better)
In your server's docroot
folder, create these files:
.htaccess
Open the .htaccess
file in your favorite text editor, type in the following, and save:
RewriteEngine On
RewriteCond %{REQUEST_URI} !handler.php$
RewriteRule (.*) handler.php
handler.php
Type the following in your .handler.php
file, and save:
<?
$over = $_SERVER['SERVER_PROTOCOL'];
$path = explode('?',$_SERVER['REQUEST_URI']);
$path = (($path == '/') ? '/home.html');
$extn = array_shift((explode('.',$path)));
$list = # array
[
'html' => 'text/html',
'css' => 'text/css',
'png' => 'image/png',
'js' => 'application/javascript',
];
$type = (isset($mime[$extn]) ? $mime[$extn] : 'text/plain');
if (file_exists(".${path}"))
{
if (is_dir(".${path}") || ($path == '/.htaccess'))
{
header("${over} 403 Forbidden");
echo "path: `$path` is forbidden";
exit;
}
header("${over} 200 OK");
header("Content-Type: ${type}");
header("Content-Length: ".filesize($path));
readfile($path);
exit;
}
header("${over} 404 Not Found");
echo "path: `$path` is undefined";
exit;
?>
With each request, get the current microtime
.
then, serve the file, (get the mime type of the file, write appropriate header: "Content-type", and simply: readfile('path/to/file.mp3');
*where 'path/to/file.mp3' would obviously be:
$_SERVER['REQUEST_URI']
or some re-routing - however you want it.
then, get the microtime again, now, subtract the former microtime from the latter, and you have the time it took to serve the file.
So, with that done, now you can log each request in the database, or where-ever, per request, specifying the field names accordingly.
I'm not sure how how much detail is required, please comment accordingly.