techcone Posted August 28, 2009 Share Posted August 28, 2009 Hello developers, I want to have a simple solution for a simple file download script. I want to prevent parallel download of files from same IP . Like rapidshare do for free user, it allows downloading of only 1 file per IP. How can I do that ? Just suggest me logic and it will be fine for me Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 28, 2009 Share Posted August 28, 2009 write a wrapper script for downloading files...let's call it download.php...and have all downloaded files go through there (this is a pretty common thing, let me know if that part is confusing) then, in your download script, before you give them the file, open a read handle on their IP address in some temp folder: $lck = fopen($_SERVER['REMOTE_ADDR'],'r+'); then, open an exclusive lock on that file: flock($lck, LOCK_EX); next, serve up the file finally, close the file handle to free up the lock fclose($lck); with the exclusive lock, any subsequent requests from the same IP (aka trying to lock the same file) will wait for the first request to finish. obviously, the person could still spoof their IP though Quote Link to comment Share on other sites More sharing options...
techcone Posted August 29, 2009 Author Share Posted August 29, 2009 Great job mate, so its just like mutual exclusion. One small problem, I close file after downloading is complete oke ? Suppose some user stops the download ? Will lock be automatically removed ? Or do I have to run a cron job every x minutes to remove such files ? Quote Link to comment Share on other sites More sharing options...
Vebut Posted August 29, 2009 Share Posted August 29, 2009 Great job mate, so its just like mutual exclusion. One small problem, I close file after downloading is complete oke ? Suppose some user stops the download ? Will lock be automatically removed ? Or do I have to run a cron job every x minutes to remove such files ? You should take a look in the manual for that function. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted August 30, 2009 Share Posted August 30, 2009 the lock is released as soon as the script ends...even if the user cancels the download ...one other note...you will end up with a folder FULL of lock files (one for every IP that has ever downloaded). you may want to write a script that goes through and cleans up those files (although, they are empty...so they won't take up much space at all) Quote Link to comment Share on other sites More sharing options...
techcone Posted August 31, 2009 Author Share Posted August 31, 2009 Yes I will assume that file is downloaded within 15 min, so will make a cron file so that cleaning process is pretty automated Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.