H
H
HaruAtari2013-12-24 22:05:53
PHP
HaruAtari, 2013-12-24 22:05:53

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

6 answer(s)
A
Andrew, 2013-12-24
@kaasius

nginx+X-Accel-Redirect
Also, Apache can do it too.

A
Alexey Zakharov, 2013-12-24
@Zakhar0v

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

O
OnYourLips, 2013-12-24
@OnYourLips

Parse web server logs.

M
maxaon, 2013-12-25
@maxaon

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.

L
lnked, 2013-12-24
@lnked

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;
}

M
Mikhail Osher, 2013-12-25
@miraage

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 question

Ask a Question

731 491 924 answers to any question