Connecting Tech Pros Worldwide Forums | Help | Site Map

how to require login for file download

Newbie
 
Join Date: Sep 2007
Posts: 23
#1: Mar 13 '08
I am using a php login system which uses sessions and a mysql database......i will have a downloads page which will only be visible if logged in....my client wants to have files on this page that can only be downloaded if logged in.

How would i do this? so even if someone typed the file url in directly they would still have to login?

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#2: Mar 13 '08

re: how to require login for file download


Well, when someone logs in you set a session, right? So, on the download page check to see if the session is present and if it isn't redirect to a login page.

[php]
if(isset($_SESSION['logged_in']))
{
// code for download page
# require("download.php");
}
else
{
// user isn't logged in
# require("log_in.php");
}
[/php]
Any questions?
Newbie
 
Join Date: Sep 2007
Posts: 23
#3: Mar 13 '08

re: how to require login for file download


but what happens if they type the file url directly into the browser?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#4: Mar 13 '08

re: how to require login for file download


Quote:

Originally Posted by ukfusion

but what happens if they type the file url directly into the browser?

Oh, i see what you mean.

Well, i've not tried my hand at this much and i've not read much (anything) about this problem. But what i would do is, have the file somewhere on your server and in a database store a name for this file (in the same row store the url)
[id - name - url_of_file]
Now, if someone wants to download the file you supply a link to a download page and in that link you pass the name from the db. On the download page you use content-disposition to force the file download. On the same page you also change the name in the database.

Aha, understand? (was abit rushed).
Newbie
 
Join Date: Sep 2007
Posts: 23
#5: Mar 13 '08

re: how to require login for file download


so basically the link name and url are taken from the db.....when the file is downloaded by someone....the link name and file name change and are re-written to the db.

what was that bit about forcing the download....didnt understand that terminology.?

i think i could manage the file rename bit so that mite work.

but i think the problem woudl be.....its ok for me adding a link and making the href come from the database...but my client will be uploading files and creating the links from their cms....which means it will just be a standard link.....i need someway of applying it directly to the file or something
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#6: Mar 13 '08

re: how to require login for file download


content disposition scroll down to the part about forcing downloads.

Can't go into the rest of your questions atm as i'm in dier need of food!

Back in 20mins :)
Reply