Hi Nuemy,
Cheers for the reply. It will take me a while to mull over this, but from what I can see I think it is exactly what I want. However, as I said I am completely new to MySQL and databases so I am very interested in line ‘Honestly, it's very dangerous to pull data from either GET or POSTS that have any direct connection to the DB’; I’, not sure I am though. I’m still working through the code, and it seems to be working fine, but I haven’t actually reached the part that I needed the above for, so I can’t really post it yet.
Ultimately, I’m trying to sell digital downloads by using PayPal’s IPN (or whatever it’s called) system. So, I have a PHP file that does all the background stuff with PayPal - takes in some information like the payers first and last name, email, and the name of the file they purchased. And, most importantly, checks if they have actually made a payment and it has processed okay.
If it has, I’m then creating a unique identifier for them (first 4 characters of their first name, first 4 of their last and 4 random characters - all of which are inserted into the DB’s primary key (which is varchar(12) as a 12 char ID for that purchase, the other cells are the file they purchased (download.zip), their email address (just cause), and a column that has the number 0 in it.
Then it emails them a download link like:
http://www.mysite.com/download.zip?token=(The primary key id)
Now, all of the above I have done and (seems) to be working correctly.
What I’m working on now, and why I was asking the above.
I have the download files in a directory (actually, a sub, sub, sub directory) with a .httpaccess file in the first directory that takes any non-standard files and forces them to process via that directory’s index page as opposed to direct linking. The index page is basically a script that forces a download dialogue for the file.
Again, this I already have working. But obviously I need to confirm that it’s an authorised user (aka one that’s paid) that’s accessing the files.
So what I want to do is grab the token id and file from the above link (both of which I can do grand), but I then want to check that A: the token ID is valid (exists in the D😎, and B: check that the file the user is trying to download is actually the file that’s in the same row as the token. And also that the column with the number 0 in it is less than or equal to 3
And then only force the download dialogue for the file is the above is true, otherwise bounce them to an error.
I’m also updating the column with the number 0 in it by 1 each time a user visit’s the download link so that the I can restrict the amount of times they download a file (similar to above, allow the download dialogue if the number is 0,1,2 or 3, but error if it’s more than three.
It’s the securest way I can think of doing this (although, god knows, I’m probably travelling from Ireland to England via America).
Although, you’re completely right I have no idea how to handle security with a data base. Most of the columns I’m writing to in the creation script have set values, the id one being varchr(12), the number one being varchar(1) and the file and email ones being varchar(200). The only access I’ll have to the database in the verifying/download page will be to check if values correspond to those in the link and updating a varchar(1) column.
As well as this, I’ve removed the mysql username and PW from the actual file and placed in a non-public directory then used an internal directory include for it
include (“/home/use/directory/file_with_passwords.php“😉;
If you can point out obvious pitfalls, security wise with using the db in this manner, please do.
Yeah, that WAS a long post