Answer the question
In order to leave comments, you need to log in
How to count the number of views of an image using php?
Good afternoon.
The page contains a number of links to various files (images). Type link
I need to count the number of downloads of these images. To be honest, I have no idea how to do it.
At first I thought of referring to a php script that will keep records and return an image with the necessary headers. But I feel that it will not be correct - to distribute statics through php.
Now the site is running on apache, in the near future I plan to transfer it to nginx.
Tell me how to keep track of the distribution of statics from the site?
Answer the question
In order to leave comments, you need to log in
one way or another, the counter values will have to be entered into the database by the script.
it only looks like this:
a link to a file (site/123.jpg) runs a script that adds a counter value and sends a header redirect to a specific file, which can generally be along the path "site/upload/563453476573465.jpg"
________________________________
upd:
www .opennet.ru/base/net/nginx_x_accel_redirect.txt.html
If you need simple statistics, then do not bother your bikes, analyze the web server logs. Wonderful thousandth question .
It is done in one line in the console.
so you can try
if( $_GET['count'] && file_exists( $_SERVER['DOCUMENT_ROOT'] ) )
{
//counter ++
file_force_download( $_GET['count'] );
}
function file_force_download($file)
{
if (ob_get_level()) {
ob_end_clean();
}
header('Content-Description: File Transfer');
if (isset($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
{
header('Content-Type: application/force-download');
}
else {
header('Content-Type: application/octet-stream');
}
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($_SERVER['DOCUMENT_ROOT'] . $file));
readfile($_SERVER['DOCUMENT_ROOT'] . $file);
exit;
}
Perverted option, and probably not the most rational, but ...
For all masked pictures, set your own log and parsetail -f /path/to/access.log
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question