cancel
Showing results for 
Search instead for 
Did you mean: 

Restricing downloads

ratbag
Grafter
Posts: 369
Registered: ‎01-08-2007

Restricing downloads

Hello,
Two things I'd like to be able to do regarding a downloadable file on my site:
1) Restrict the connections from each person downloading.
2) Put a limit on the number of times the file can be downloaded per day (to prevent my site being removed again).
Anyone done this or know of any resources that may help? I saw an article on the web regarding problem 1, but it used mySql and though I'd like to get into it one day, I have no experience with it at the moment. I can cope with php though.
Thanks for any advice,
Barry.
8 REPLIES 8
Tony_W
Grafter
Posts: 745
Registered: ‎11-08-2007

Re: Restricing downloads

@Barry
I posted 2 years ago about restricting the connections from each person here suggesting mod_throttle and mod_bandwidth.
I had earlier come close to having my business site removed by someone downloading a less than 1MB file and using up 147MB(if I remember correctly) doing it. See the post before
I later posted here on the subject as someone else used lots.
I can, using PHP, monitor the number of downloads and limit each IP address to 6 clicks, but not the number of connections and 'part-downloads'.
I did not get anywhere with PN installating connection limiters even though it would help prevent denial of service attacks against them.
Hope this helps....
Tony
ratbag
Grafter
Posts: 369
Registered: ‎01-08-2007

Re: Restricing downloads

Thanks Tony. Seems you didn't get any replies to your posts.
Perhaps someone reading this can help.
Barry.
Tony_W
Grafter
Posts: 745
Registered: ‎11-08-2007

Re: Restricing downloads

Best of luck Barry, but it is in PN's hands.
There is no way for you to detect (or act upon) the number of connections used to download from your site.
PN will punish you for something that you can do nothing about.
Tony
ratbag
Grafter
Posts: 369
Registered: ‎01-08-2007

Re: Restricing downloads

Thanks again Tony.
Perhaps I can try restricting the number of downloads to a sensible amount per day and see how it goes.
I'm new to PHP but know 'c' which seems very similar. I created this function today; perhaps someone would be good enough to pick holes in it for me, as it's easy to miss something obvious on your own:
EDIT: updated already, saw something obvious:

function canDownload($fileName, $maxTotalDownloads, $dbFile)
{
  $data;
  $canDownload = false;
 
  if(file_exists($dbFile))
  {
    if(is_writable($dbFile))
    {
      $data = unserialize(file_get_contents($dbFile));
     
      if($data['date'] == date('Y m d'))
      {
        // check if max count has been reached
        if($data['today'] < $maxTotalDownloads)
        {
          // increase the count of the specified file and the total
          @$data[$fileName]++;
          $data['total']++;
          $data['today']++;
       
          // allow file to be downloaded
          $canDownload = true;
        }
      }
      else
      {
        // new day; reset date and counter
        $data['date'] = date('Y m d');
        @$data[$fileName]++;
        $data['total']++;
        $data['today'] = 1;
        $canDownload = true;
      }
      // write new values back to database file
      file_put_contents($dbFile, serialize($data));
    }
    else
    {
      // database not writable (CHMOD 600)
      echo 'database not writable <br />';
    }
  }
  else
  {
    // database file doesn't exist
    echo 'database file doesn\'t exist <br />';
  }
  return $canDownload;
}
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: Restricing downloads

Is the intention to have a single limit for all files or an individual limit for each file? As coded the former applies. If you intended it to be the latter, the 'today' values for each file need to be stored with the filename total as a second dimension, the filename being the first dimension. Or would you want both filename and daily totals limited to minimise risk of overshooting the traffic limit?
I think the preferred way to keep this data would be in a MySQL table rather than a file.
David
David
ratbag
Grafter
Posts: 369
Registered: ‎01-08-2007

Re: Restricing downloads

Hello David, thanks for checking the code, much appreciated.
Quote
Is the intention to have a single limit for all files or an individual limit for each file?

A single limit for all files, the other totals are just for curiosity really.
Quote
I think the preferred way to keep this data would be in a MySQL table rather than a file.

Yes, I suppose I really aught to look into that but I know absolutely nothing about it at this stage. I need to get a book on PHP and MySQL really. I'll probably get it up and running using a file for now and change it once I know more about it.
Thanks again,
Barry.
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: Restricing downloads

One potential problem you might have is with the file appearing to disappear if two downloads are attempted simultaneously (the second visitor seeing a missing-file error). To avoid that the file needs locking until the write is complete. Use of the file_put_contents LOCK_EX flag introduced in PHP 5.1 should avoid that problem. (Note: I haven't tried file locking with that function.)
David
David
ratbag
Grafter
Posts: 369
Registered: ‎01-08-2007

Re: Restricing downloads

Thank you David,  I'll go and read up about that.