I'm designing a website for a small indie record label and they've dropped a bombshell asking if I could implement a function where a user can enter a code to receive a digital download.

Is there a simple solution to doing this? I was thinking all I would need is an input field where the user can enter a code, it gets verified and then allows a download but it sounds too simple. Is this even possible with something like .php (complete beginner)?

I'm willing to learn or I would've packed it in already so any advice would be great. Thanks.


Edit:

Thanks to some great advice I was able to create this!

有帮助吗?

解决方案

If you wanted to do it at a very simple level, it is not much more than you describe it to be. You would need to familiarize with PHP and MySQL or some other database, but it isn't too difficult to create.

You need to figure a few things out, such as how do you want to limit the codes, 3 downloads in the first 24 hours to allow for failed downloads, restrict it to IP, or strictly one full download. Since you have the list of the 1000 codes given, you will probably want to base your system around having codes pre-generated and inserted in the database, rather than having an algorithm that can validate the codes on the fly and then check for already used codes.

You would want to store the download(s) in a directory that is not accessible from the web, and the php script would validate the code, and if valid serve the download to the user.

You can probably look to the wordpress plugin's database structure for other ideas, but I think at the very least you would need:

download_code   (the code itself, probably primary key or at least index)
download_file   (optional, the name/path of the file this code allows them to download)
redeemed        (0 if not redeemed, 1 if redeemed)
redemption_time (timestamp of the first, or last redemption based on your requirements)
download_count  (how many times downloaded if allowing more than 1)
ip_address      (ip address of the redeemer)
email_address   (email address if you want to collect it, or give user the option)
download_url    (the unique string for the download url.  this could be one way to implement   download verification beyond the code, but is not necessary)

You would then need to create an html page with the text box for entering the code, and any other optional data you wish to collect. The form would submit to your PHP script.

When the PHP script receives a form post, it would validate all of the data (i.e. email address if you were collecting it). Once all data is valid, you read from the database looking for a code matching what the user entered.

If no data was found with the code, send them back to the form to try re-entering it. If a record is found, you can check the redeemed value from the database and see if the code has been used or not. If it has, this is where you can use custom logic to decide if they are still within their download window, the ip address is the same, or whatever criteria you want to use to allow re-downloads.

If it has been redeemed, show an error message. If it is still okay to download, you can serve a download by reading the file and sending it to the browser see example #1 here.

At some point you will have to update your database to set the redeemed flag to 1 and update the other values such as timestamp and download count. You can either run this code before you serve the download, or you can run it after the download is served. In some cases if the download was cut off, the last portion of your script won't run and therefore won't update redeemed or download_count. This may or may not be what you want, so you can decide where you want to do the updating.

Eventually you can update it to include an administration panel, but in the beginning all configuration could be done within the php script or config file. And eventually you could update it to use flash or some other technology to download the file and show progress bars etc.

Hopefully that will give you some idea on whether or not you want to try to implement it. Otherwise you could always search php on Hotscripts to see if there is an already rolled standalone version of what you want.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top