Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    199
    Thanks
    78
    Thanked 5 Times in 5 Posts

    how to limit user downloads to one simultaneous download

    Hi guys,

    Im not really sure what the best approach for this is but i need some help to build a script that can limit downloads to one simultaneous download per user.
    To ensure that all users are able to get reasonable speeds while downloading.

    Honestly im not even sure if I can do this in php but im hoping its possible.
    Don't get me wrong Im not asking for a compleet script.
    Just what kind of steps I need to take to make it work.
    Mabey some tutorials so I can learn the basics.

    Currenlty i've made a database and a download script so people can request downloads from my database but what do I do next?

    I was thinking mabey I can store the session from the user inside my database and if there session is downloading then deny access to other files.
    But how would I know when somebody has finished there downloads and how do I know if a user clicks on cancel because he/she changed there minds while requesting the file.

    Thanks for any help.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Easiest method is to use a unique key in each download link. It should work something like this:

    When someone visits your downloads page, generate a random key using uniqid()

    Put that into each download url and store in a table called keys - which has a 'used' column.
    When a user clicks it to download a file, that key from the $_GET request is checked in the keys table to see if its been used. If 1 its been used already, if 0, it hasn't so if its 0 then the download can begin and the keys used column updated to 1.

    Read the file and print it to the user using readfile().

    As for knowing when a download is finished, the only real way to do it is to read the file in a loop and write out each KB. Using register_shutdown_function() you can then determine if the script wrote out all of the file or just part of it and adjust the 'used' column for the key accordingly. This method is quite complex and involves some very skilled methods of debugging - something I know only too well from my own download code.
    Last edited by tangoforce; 01-27-2012 at 08:26 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    have a downloding table in the db that has userid, dl_start, dl_end fields or something like that...

    when the user starts a download insert a record in the userid & dlstart to signal they have a download in progress and update dl_end when it finishes

    then when starting a new download check if there is any open dl_start without a corresponding dl_end for that user, that way would keep a record of their downloads too...

    if you just wanted a stateless approach you could have inprogress field that you update to 1 if a download is in progress and update to 0 when it ends

    you could even just add it as a field in the user details `dl_inprogress` field and update that when they start/finish a download

    how to know if they are finished?
    checkout these descriptions
    http://stackoverflow.com/questions/1...-a-file-in-php
    http://stackoverflow.com/questions/1...-send-the-file

    basically you need to tunnel the download through a separate script so when the visitor downloads a file they actually connect to your script and your script gets the actual file and feeds it to them. then by monitoring how much of the file you are currently reading you know how much the user has downloaded so far...

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by jmj001 View Post
    have a downloding table in the db that has userid, dl_start, dl_end fields or something like that...

    when the user starts a download insert a record in the userid & dlstart to signal they have a download in progress and update dl_end when it finishes

    then when starting a new download check if there is any open dl_start without a corresponding dl_end for that user, that way would keep a record of their downloads too...

    if you just wanted a stateless approach you could have inprogress field that you update to 1 if a download is in progress and update to 0 when it ends

    you could even just add it as a field in the user details `dl_inprogress` field and update that when they start/finish a download
    What happens then if its a site that is open to the public? - None of them will have a unique user ID. You've missed the point, there must be something unique to identify the user to stop multiple download threads from download managers. Thats where the unique key I mentioned comes into play.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    I don't think this is possible in PHP. You'd have to keep track of IP addresses, and check if it is already downloading a file. As far as I know there's no way of determining what has happened to the file once the request has left the server. The only solution I can think of includes sockets, but I'm not an expert in PHP so I couldn't really give you a good place to start.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by iLochie View Post
    I don't think this is possible in PHP ... but I'm not an expert in PHP so I couldn't really give you a good place to start.
    Yes it is possible, I've done it, I have working code which can not only restrict the number of connections but support pause/resume, limit the download speed and also disconnect the client.

    As you said, you're no expert in PHP so why have you even commented on this when there are two replies clearly explaining how to do this and that it is definitely possible?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by tangoforce View Post
    Yes it is possible, I've done it, I have working code which can not only restrict the number of connections but support pause/resume, limit the download speed and also disconnect the client.

    As you said, you're no expert in PHP so why have you even commented on this when there are two replies clearly explaining how to do this and that it is definitely possible?
    Your lack of politeness is offensive. However, I had skipped past your mention of the register_shutdown_function method because you completely missed his intentions in the first half of your post. Thank you for introducing me to this method though, could come in handy in the future.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by iLochie View Post
    Your lack of politeness is offensive.
    Huh? I get straight to the point. This isn't really a social forum, it's one for passing tips, ideas and helping to troubleshoot. It's not about trading life stories.

    Secondly, you're the one who's skipped the previous replies and randomly dragged up a topic that you admit you don't even know the answers to - for what reason? - You say I have a lack of politeness?

    Quote Originally Posted by iLochie View Post
    However, I had skipped past your mention of the register_shutdown_function method because you completely missed his intentions in the first half of your post. Thank you for introducing me to this method though, could come in handy in the future.
    No, I missed nothing. I made it quite clear that you need a unique token per visitor so that you can track who is trying to access what file. IF you use a download manager such as flashget that tries to use 5 different http threads to download the same file it will use the same token in the url for each thread. Therefore you can conclude that you have 5 matching attempts at the same file and not 5 different users.

    Secondly, register_shutdown_function is for tracking the status of the download - finished or unfinished. Nothing else.

    You are saying I've completely missed the point of the ops post? - I've provided the tips the op asked for and you are the one who has then derailed it saying that it can't be done and that you don't even know PHP well enough to really comment on it anyway yet you criticise my answer as not providing enough information.

    Let me tell you something, the reason you don't think it contains enough information is because you don't understand it (as you've already admitted). I'm not going to sit here and write code for the user when I can give them the tips they need to go off and do it themselves.

    Finally, when conware hasn't actually come back and even acknowledged the replies, how can YOU judge that I've not understood the request?

    This is all factual points. Any 'tone' or 'offence' is completely unintended. Just read it for what it is point by point.

    Edit:
    IF you still wish to believe that I know nothing about what I'm talking about, download TeamViewer and I will show you PHPs download management capabilities on my own desktop. I will show you how to use a database to manage, check, restrict everything however I will not show you the actual code.
    Last edited by tangoforce; 01-29-2012 at 06:44 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    Quote Originally Posted by tangoforce View Post
    What happens then if its a site that is open to the public? - None of them will have a unique user ID. You've missed the point, there must be something unique to identify the user to stop multiple download threads from download managers. Thats where the unique key I mentioned comes into play.
    umm.. ok.. uniqueid could be stored in a cookie on the visitors browser... then if they try another download you check the cookie and check the db to see if their previous download is completed....

    you'd need to have a process whereby you knew what the status of the download was... tunneling through another script seems like the best way to do that in my opinion...

  • #10
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    Quote Originally Posted by iLochie View Post
    I don't think this is possible in PHP. You'd have to keep track of IP addresses, and check if it is already downloading a file. As far as I know there's no way of determining what has happened to the file once the request has left the server. The only solution I can think of includes sockets, but I'm not an expert in PHP so I couldn't really give you a good place to start.
    it's definitely possible... as far as knowing if a file is downloaded or not you simply need to run it through another script which gets the file and delivers it to the browser...

    the browser clicks thisfile-1.php and thisfile-a.php goes and gets realfile-to-download.mov and presents it to the browser....

    because thisfile-a.php is in the middle it can know how much of the realfile-to-download.mov it has read and delivered to the browser...

  • #11
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by tangoforce View Post
    Huh? I get straight to the point. This isn't really a social forum, it's one for passing tips, ideas and helping to troubleshoot. It's not about trading life stories.

    Secondly, you're the one who's skipped the previous replies and randomly dragged up a topic that you admit you don't even know the answers to - for what reason? - You say I have a lack of politeness?



    No, I missed nothing. I made it quite clear that you need a unique token per visitor so that you can track who is trying to access what file. IF you use a download manager such as flashget that tries to use 5 different http threads to download the same file it will use the same token in the url for each thread. Therefore you can conclude that you have 5 matching attempts at the same file and not 5 different users.

    Secondly, register_shutdown_function is for tracking the status of the download - finished or unfinished. Nothing else.

    You are saying I've completely missed the point of the ops post? - I've provided the tips the op asked for and you are the one who has then derailed it saying that it can't be done and that you don't even know PHP well enough to really comment on it anyway yet you criticise my answer as not providing enough information.

    Let me tell you something, the reason you don't think it contains enough information is because you don't understand it (as you've already admitted). I'm not going to sit here and write code for the user when I can give them the tips they need to go off and do it themselves.

    Finally, when conware hasn't actually come back and even acknowledged the replies, how can YOU judge that I've not understood the request?

    This is all factual points. Any 'tone' or 'offence' is completely unintended. Just read it for what it is point by point.

    Edit:
    IF you still wish to believe that I know nothing about what I'm talking about, download TeamViewer and I will show you PHPs download management capabilities on my own desktop. I will show you how to use a database to manage, check, restrict everything however I will not show you the actual code.
    Hahah, what are you trying to prove? I understand you know what you're doing, you seem to have some sort of self confidence problems though? At any rate, I figured the part about the unique IDs was just a misunderstanding. Knowledge comes from experience, I haven't worked with a downloading script before, and I haven't studied PHP documentation religiously so I didn't know about these capabilities.

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by iLochie View Post
    Hahah, what are you trying to prove? I understand you know what you're doing, you seem to have some sort of self confidence problems though? At any rate, I figured the part about the unique IDs was just a misunderstanding. Knowledge comes from experience, I haven't worked with a downloading script before, and I haven't studied PHP documentation religiously so I didn't know about these capabilities.
    So why did you reply to this thread then? - Did you just pick a random thread and say to yourself "I know, I'll say something I know nothing about just to derail it" ?

    As for proving anything, no I'm not. I don't need to, I KNOW it is possible. As for confidence, I can confidently say that I know what Conware wants is possible which is more than you could confidently say.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by jmj001 View Post
    umm.. ok.. uniqueid could be stored in a cookie on the visitors browser... then if they try another download you check the cookie and check the db to see if their previous download is completed....
    No, you just need it in the url to the file. If using a download manager (or even just the browser) the id/token will be the same for the multiple threads.

    Quote Originally Posted by jmj001 View Post
    you'd need to have a process whereby you knew what the status of the download was... tunneling through another script seems like the best way to do that in my opinion...
    I've no idea what you're talking about tunneling through another script for, it can all be done through one script. The request goes to that one script, it checks the DB/session, reads the file and outputs it. At the end, using register_shutdown_function it records into the database whether the file download was completed or not.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    ok cool..

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,421
    Thanks
    62
    Thanked 535 Times in 522 Posts
    Quote Originally Posted by jmj001 View Post
    the browser clicks thisfile-1.php and thisfile-a.php goes and gets realfile-to-download.mov and presents it to the browser....

    because thisfile-a.php is in the middle it can know how much of the realfile-to-download.mov it has read and delivered to the browser...
    No, no, no... thats going to eat server resources by relaying it from one script to another like that.

    Do it all in one script and the job is done.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •