View how many people accessed pdf

Asked

Viewed 229 times

2

I have a pdf on my server and I put it available through a link, there is way to control how many people access the pdf?

  • Voce uses a database?

  • Yes mysql database usage.

  • make an account clicks ex: if clicknolink update table +1

  • And if the link is not on the site? or if people only sign in if I give the link?

  • try down what I posted

  • Suggestion makes an ajax jquery with click event and takes the id of that file and makes a php file with update ai each click triggers the jquery that makes an ajax in that file with post or get

Show 1 more comment

2 answers

1

1 phase - collecting accesses and displaying the file

// abrir o arquivo através do link do seu servidor
$file = 'file.pdf';
// nome do arquivo
$filename = 'file.pdf'; /* Nota: sempre use .pdf no final. */

// Conectando ao banco
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
// Realizando a query
mysqli_query($link, "UPDATE acessos SET contador = contador+1 WHERE `nomearquivo` = $filename);

// exibindo o arquivo
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);

2 phase - see total accesses

   // essa parte vc pode coloca no local que vai aparecer o total
   $link = mysqli_connect("localhost", "my_user", "my_password", "world");
   $result = mysqli_query($link, "select contador from acessos where `file` =  $filename);
   if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "$row["contador"]";
    }
  • Only an opinion, instead of read file, maybe using while, fgets and feof may be better for the memory issue, I believe.

-2

Now if you want a quick fix

First choose your code in one of those that generates online

ex: http://contador.s12.com.br/

2º create a file ex: contacliques.html

and put in it

<meta http-equiv="Refresh" content="0; url=http://linkdopdf>

codigo do contador que pegou no site

remembering that this is not exact and only serves to have control of 1 file only

  • if you want you can change the content= as much of seconds as you want it to appear if you want to view it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.