0

What im trying to do is, on my website I want to show a counter that each time someone presses the download button on the page (which is already present) a counter increases. This counter should not be stored locally, but on the server. So that the counter increases for everyone not just for one user. What would be the best way to aproach this? Thanks for your help in advance

3 Answers3

0

The server can not track if a button is clicked but often when a button is clicked a request is made to the server.

If you made a download.php that would send the file back this also gives you the opportunity to increase a counter.

This number has to be saved on the server. either in a database or in a file.

This question is also asked and answered here: PHP making a download counter without leaving the current page

it includes example code.

dehart
  • 1,554
  • 15
  • 18
0

You can achieve it easily by having a robust backend coupled with a Database or cache (like redis).

On the button click, you can create an even listener which will have to send a trigger to your backend. Your back end needs to handle further logic either by incrementing a value from your Database or increment the value stored in a cache. For this use case I would prefer a cache like redis.

For a static counter

If you want to show a static counter which just shows a static value whenever the page is opened, then you can add a module to fetch the count from the DB or cache on the occurrence of the onload event.

For a live counter

If what you are trying to achieve is a live counter which updates dynamically with no explicit event triggers, then you need to go for JS WebSocket coupled with a Worker.

Reference:

WebSocketAPI - https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

Worker API - https://developer.mozilla.org/en-US/docs/Web/API/Worker

It Assistors
  • 998
  • 2
  • 14
  • 29
0

Put the following code into a file called counter.php

Each line of the PHP code is itself describing it's own importance

<?php

 $counter = 'path/to/counter.txt';      // text file to store download count - create manually and put a 0 (zero) in it to begin the count
 $download = 'http://mywebsite.com/file/to/download.zip';    // the link to your download file

 $number = file_get_contents($counter);    // read count file
 $number++;                                // increment count by 1
 $fh = fopen($counter, 'w');               // open count file for writing
 fwrite($fh, $number);                     // write new count to count file
 fclose($fh);                              // close count file
 header("Location: $download");            // get download

?>

Create the text file defined in the $counter having a location as described value/location above and just put a 0 (zero) in it. The sample code file is called counter.txt.

Create a download link to the counter.php file instead of the actual download file

Your download button code

<a href="path/to/counter.php">DOWNLOAD</a>

To display the download count on your web page, simply put this code on that particular page

<?php echo file_get_contents('path/to/counter.txt');?>
Fareed Khan
  • 2,613
  • 1
  • 11
  • 19