Jump to content

[SOLVED] stop a user typing in a direct path to download a file


jesushax

Recommended Posts

hi all,

i was wondering how id go about stopping a user typing in a direct path to download a file, i want it so they have to be logged in then click the link from a logged in page only then will it download

 

anyone help me out?

Cheers

Link to comment
Share on other sites

no i dont want to force them to download

 

i want to stop anonymous users from typing in a file path and being able to download a file

if they are a registered users they can click downloads then it will work

 

so if someone typed in the url www.site.com/downloads/videos/thisvideo.mpg it would say not allowed your not a member

 

but if you went to www.site.com logged into members area then clicked downloads > videos > thisvideo.mpg they would e allowed

 

Thanks

Link to comment
Share on other sites

no i dont want to force them to download

 

i want to stop anonymous users from typing in a file path and being able to download a file

if they are a registered users they can click downloads then it will work

 

so if someone typed in the url www.site.com/downloads/videos/thisvideo.mpg it would say not allowed your not a member

 

but if you went to www.site.com logged into members area then clicked downloads > videos > thisvideo.mpg they would e allowed

 

Thanks

 

In order to do that the easiest way is to store the files outside of your web root, then use a force download script to allow logged in members to retrieve the files. I don't make suggestions if I don't think there not valid (most of the time anyways).

Link to comment
Share on other sites

i dont think i can do that, im not in control of our web host :|

 

Why don't you check with your host to see of you have access to a directory outside of your web root? If you don't, I'd be looking for a better host.

Link to comment
Share on other sites

Your web host should allow you to stores files outside of the web root. When you login through an FTP client, do you have to drill through to "htdocs" or "public_html" (or something similar)?

Link to comment
Share on other sites

ive emailed my host, hopeully theyll get a folder set up for me

 

regards to the script for logged in users what would this look like?

 

Cheers

 

That's not something thats going to be covered in a simple forum reply. Should be literally thousands of examples of php login scripts around if you search.

Link to comment
Share on other sites

oh right...

 

so i create ahyperlink to the force download page and that will open the file i want it too

 

what do i edit to make it choose the right file, i can see something commented out but i don tknow what that means

 

<?php

// force to download a file
// ex, ( [url=http://localhost/php/download.php?file=C:/Apache]http://localhost/php/download.php?file=C:/Apache[/url] Group/Apache2/hongkong.php )
// hope this can save your time :-)

$file = $_REQUEST['file'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);

?>

Link to comment
Share on other sites

I'll give you an example.

 

download.php

<?php

session_start();

// check user is logged in.
if (!$_SESSION['islogged'])) {
  die("You do not have permission to download files");
}

$file = $_GET['file'];

// the absolute path to your directory storing your files.
$path = '/home/username/files/';

$download = $path.$file;

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".$download);
header( "Content-Description: File Transfer");
@readfile($file);

?>

 

Now, given the address http://yoursite.com/dowload.php?file=foo.jpg your logged in user would get the file located at /home/username/files/foo.jpg. Comparatively, your web root would be within /home/username/htdocs or somewhere similar.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.