Jump to content

Multiple Download From Same IP


techcone

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/172298-multiple-download-from-same-ip/
Share on other sites

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

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 ?

 

 

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.

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.