How can i limit a download on a file?

Discussion in 'Site & Server Administration' started by pakh, Dec 31, 2007.

  1. #1
    I got a file on my server, for example: http://www.site.com/file.zip

    I want to limit the download on the file to 20 downloads, and after that it cant be downloaded. How can i do that?

    Thanks.
     
    pakh, Dec 31, 2007 IP
  2. mysweetjane

    mysweetjane Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi there, you can limit downloads using zen shopping cart - free software on cpanel or by using a software like DL Guard - it is $127 and you can use it on two domains also as a membership site.

    Hope that helps :)
     
    mysweetjane, Dec 31, 2007 IP
  3. pakh

    pakh Peon

    Messages:
    303
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I dont think that is what i want...I might be wrong..

    still looking for something that does what i want.
     
    pakh, Jan 1, 2008 IP
  4. chrissyj

    chrissyj Peon

    Messages:
    56
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If I understand you correctly, you want to limit the download of each file to some number of downloads (like 20). The only way to do what you are asking about is thru a script on the server. How are people downloading files now? Is it thru a link on a web page? If so, what scripting language are you using? PHP. Perl, CGI, something else?
    You can PM me with further info, if you like.
     
    chrissyj, Jan 2, 2008 IP
  5. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    pakh: I coded a small PHP program to watch for the GET and tag +1 to a counter in mysql based off IP address.

    Very simple program and I would recommend doing it this way.

    Zencart and other solutions are valid as well but I am a homebrew guy myself.

    You can watch for various things. Obviously if your watching via IP address then they can change hosts and download some more.

    On my Fre FOnts site i watch for 5 downloads then force them to signup in which they get unlimited downloads.

    All free. Just basically getting the email addy from them.
     
    LittleJonSupportSite, Jan 3, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    It depends on what you already have to work with.
     
    joebert, Jan 3, 2008 IP
  7. mellow-h

    mellow-h Peon

    Messages:
    750
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you are using apache, then you can do it with mod_limitipconn module. This module allows you to limit downloads from a location on the server per IP based. :)
     
    mellow-h, Jan 4, 2008 IP
  8. nyunyu

    nyunyu Banned

    Messages:
    710
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #8
    And if you are using vbulletin as the download medium, there is a plugin called Link And Download manager which you can set whatever you want including limit per day/month/year etc.
     
    nyunyu, Jan 4, 2008 IP
  9. pakh

    pakh Peon

    Messages:
    303
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i am not using anything special, just using the direct link: site.com/file.zip

    But if i can do something to the file so it doesnt get downloaded more than 20 times.

    What is the best way of doing this?

    Anything easy, as i am not an expert on php, apache, etc
     
    pakh, Jan 5, 2008 IP
  10. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #10
    The way I would do it is to add a rewrite rule that called a php script rather than the actual file.

    RewriteRule /file.zip /downloadfile.php [NC,L]
    Code (markup):
    Then, in the downloadfile.php I would put some code that checked the number of times the file had been downloaded and either give them the file or give them an error message. Something like this should work but beware, I am writing this code without running it... it may not compile :p You will definitely want to look up the correct mime-type header for zip files because I have no idea what it should be.

    <?php
    // If the counter is more than 24 hours old, zero it.
    if(filemtime("counter.txt") - time() < (60 * 60 * 24))
    {
      file_put_contents("counter.txt", "0");
    }
    
    // Find out how many people have downloaded today.
    $counter = file_get_contents("counter.txt");
    
    // If more than twenty, give an error message.
    // If 20 or fewer downloads, give them the file. 
    if($counter > 20)
    {
      include("too_many_downloads.html");
      exit();
    }
    else
    {
      // Increment the file counter...
      file_put_contents("counter.txt", $counter + 1);
    
      // Send the right mime-type header.
      header("Mime-type: application/zip");
    
      // Send the file.
      echo(file_get_contents("file.zip"));
    }
    ?>
    
    PHP:
    Don't forget, PHP.net is your friend.
     
    Ladadadada, Jan 6, 2008 IP
  11. Marton

    Marton Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Just tried the code above as I need exactly the same as pakh. I get a "PHP Fatal error: Call to undefined function: file_put_contents() in C:\Servers\WWW\Root\download\get.php on line 5". Any clues? I'm using PHP4.
     
    Marton, Jan 11, 2008 IP
  12. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #12
    file_put_contents isn't available before php5. You'll have to use one of the emulated functions found in the comments on the manual page.
     
    joebert, Jan 11, 2008 IP
  13. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #13
    
    
    <?php
    if (!function_exists('file_put_contents')) {
    
        function file_put_contents($filename, $data, $respect_lock = true)
    
        {
    
            // Open the file for writing
    
            $fh = @fopen($filename, 'w');
    
            if ($fh === false) {
    
                return false;
    
            }
    
    
            // Check to see if we want to make sure the file is locked before we write to it
    
            if ($respect_lock === true && !flock($fh, LOCK_EX)) {
    
                fclose($fh);
    
                return false;
    
            }
    
    
    
            // Convert the data to an acceptable string format
    
            if (is_array($data)) {
    
                $data = implode('', $data);
    
            } else {
    
                $data = (string) $data;
    
            }
    
    
    
            // Write the data to the file and close it
    
            $bytes = fwrite($fh, $data);
    
    
    
            // This will implicitly unlock the file if it's locked
    
            fclose($fh);
    
    
    
            return $bytes;
    
        }
    
    }
    
    ?>
    
    
    Code (markup):

    Use this function in a file or include it from another file.

    Enjoy!
     
    LittleJonSupportSite, Jan 11, 2008 IP
  14. pakh

    pakh Peon

    Messages:
    303
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Sorry, but i dont understand...

    Do i put this code in a .php file? Where do i edit the filename in that code?
     
    pakh, Jan 12, 2008 IP
  15. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Put it in the same .php file as the rest of the code I posted earlier.
     
    Ladadadada, Jan 13, 2008 IP
  16. pakh

    pakh Peon

    Messages:
    303
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I have sent you a PM
     
    pakh, Jan 13, 2008 IP