Create a unique download link only once

I want to create some unique download links for my users. The reason is because I wanted to let them download only once so that they could use the same download link again.

I create several keys (for example, qwertyasdfghzxcbn. As in the download link, it will be like www.xxxxx.com/download.php?qwertyasdfghzxcbn) in the database and in the flag field, where, when the user has downloaded, he will update 1 to the field the flag.

I did a web search and found this. http://www.webvamp.co.uk/blog/coding/creating-one-time-download-links/

But this only works when you go to the page, then only the page will generate a unique link. I have already pre-created the link inside my database, I do not need to restore it again, if the fact, if I generate the key when the user goes to the page, they will be able to download several times, updating the page.

+8
php
source share
3 answers

The solution would be to make the link the very goal of the PHP script.

You would hide the actual file somewhere inaccessible from the browser (i.e. where you can get to the file via fopen() , but not at the root of the document) and place the download.php file to upload the files.

The loading script will look something like this:

 $fileid = $_REQUEST['file']; $file = file_location($fileid); // you'd write this function somehow if ($file === null) die("The file doesn't exist"); $allowed = check_permissions_for($file, $fileid) // again, write this // the previous line would allow you to implement arbitrary checks on the file if ($allowed) { mark_downloaded($fileid, $file); // so you mark it as downloaded if it single-use header("Content-Type: application/octet-stream"); // downloadable file echo file_get_contents($file); return 0; // running a return 0; from outside any function ends the script } else die("You're not allowed to download this file"); 

Any link you point to just points to download.php? fileid = 712984 (no matter what the file actually is). This will be the actual download link, as the script transfers the file; but only if the user is allowed to receive it. You will have to write the functions file_location() , check_permissions_for() and mark_downloaded() .

+9
source share

I know this question is getting a little old, so I apologize for adding to it now, but if you are comfortable working with the API, I built http://linkvau.lt this is an easy way to create one-time download links. Since this is a JSON API, it can be made to work with any language, but I also provide a simple PHP library for interacting with the API.

+1
source share

I would suggest using the uniqid () function and storing unique identifiers with an expiration date in the database, returning to the user url using something like this: ...? file_id = $ id

When the link is open, you can delete it from the database or mark it to be deleted β€œsoon” (just in case, the user wants to refresh the page.)

0
source share

All Articles