0

I need to limit the number of times a pdf is downloaded by people (to 500). Ideally it would count the clicks to 500 and then remove the link. It has just occurred to me that I will also need to stop each person clicking more than once. Basically I have been asked to allow the first 500 people download a file and then end it. PHP or javascript would be prefered (its on a wordpress site)

regards

Rich

3 Answers3

5

You don't wanna limit the number of times a link can be clicked, you want to limit the number of times that particular link returns a PDF file.

In other words, your link shouldn't point directly to the requested resource (the PDF), but to a PHP file that can assert that the file hasn't been downloaded more than 500 times.

Here's an example of how to output files outside your www directory to the browser.

Community
  • 1
  • 1
Sherlock
  • 7,525
  • 6
  • 38
  • 79
0

Link to a PHP script that will control the access and serve the PDF if allowed (example with readfile(). Use the $_SESSION to manage how many times a single user has downloaded - update a value after downloading.

Use a database table or other data source to count the total downloads so that when it reaches 500, you can deny all access to the PDF.

MrCode
  • 63,975
  • 10
  • 90
  • 112
0

To expand on Sherlock's solution, you want to hide your PDF behind a PHP file. When I say hide, I mean placing it in a place that is not accessible directly: consider moving it out of your "www root" or using a .htaccess to prevent accessing the file directly. Most advanced users will figure out how to access your PDF if the document is guessable and publicly available.

Your PHP should simply do the following:

  • Check if the document has been downloaded less than 500 times OR the user's IP has "unlocked access" to the document (you might want to allow a "window" during which a user can download again the document - some people will open the file when they actually want to save it, and don't know how to save to disk from their reader) - otherwise display an error
  • Store the IP address of the user for this document
  • Send the appropriate header for the file type: header('Content-type: application/pdf');
  • Send the file name: header('Content-Disposition: attachment; filename="the document.pdf"');
  • Send the file content: readfile($pathToPDF);
emartel
  • 7,712
  • 1
  • 30
  • 58