What is the best approach for implementing a single-use password to download a file? Initially I thought about using PHP, assigning a password to a user and, upon them logging in removing the account. I would keep the account in a file as I don't expect to ever have more than a handful of valid users at any given time. Do I need to track sessions or is there a simple way to accomplish this with a single site?
Oh, I cannot use an external site as the data is sensitive and must be kept locally nor can I download any new software and thus am limited to HTML, JavaScript and PHP (I believe).
Pretty much just a combination of Ilmari Karonen's and martinstoeckli's answers, but with more detail.
Using this sort of database table:
temp_passes
uuid | createTime | expireTime | filename
------------------------------------------
123-4 | 1326735047 | 1327735047 | myDisk.iso
Somehow, you generate a UUID for the user, and insert it into the database. Then, when giving a download link, you use the UUID like so:
http://example.com/download.php?id=123-4
or alternatively, have a password field where the user has to enter in their ID, and submit it to download.php. Either way:
readFile().You can choose to delete the ID either before or after the download has finished, that's up to you. However, if it's a large file, you'll likely want to do it first so that other people cannot use the same "password" to have multiple simultaneous downloads.
You could just write a simple PHP script that checks the password against a hash stored on the server (either in a file or in a database) and, if it matches, delivers the file using readfile(). Once you've successfully sent the file, remove the password from the list or mark it as disabled somehow.
(Yes, I realize that this answer is very vague and generic, but then, so is your question too. If you can clarify what it is that you're unsure about, I might be able to give a better answer.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With