SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Password protect an entire directory

    Anyone know if is it possible to password protect an entire directory in PHP so if a user enters the filepath to a word doc/ pdf/excel doc or whatever else in that directory the cannot access it without being logged in?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The most simple way would be to create a RewriteRule in a .htaccess file that looks for the specific URL and takes the user to an authentication page along with the query string appended to the URL, if the user has a current session with the site then grab the download using the query string passed into the RewriteRule.

  3. #3
    SitePoint Enthusiast jakub_polak's Avatar
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can solve this in that way, that you put all files that you want to allow only certain users to download outside of web root and then you access the file with PHP script in which you control if the user is or is not logged in.

    This problem is discussed on many websites, for example here http://bytes.com/topic/php/answers/7...le-not-webroot

  4. #4
    SitePoint Member WebMonkeyOttawa's Avatar
    Join Date
    Jan 2012
    Location
    Ottawa, ON
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a file in the dir you want to restrict named .htaccesswith only this: deny from all. Now all the content in this folder will be unobtainable. Now create a PHP script to manage authentication (username/password access). When a user is logged in and selects a file to download, use PHP to grab the file and output it. Simple process where you have to send header with the right filetype identified, then just dump the contents of the file.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds perfect.
    You couldn't just explain this bit a touch more or point me to where I can find out could you?
    "create a PHP script to manage authentication (username/password access). When a user is logged in and selects a file to download, use PHP to grab the file and output it. Simple process"

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Davecheet,

    If you want a set of login scripts, you can google something like PHP login. I actually started my login scripts with information I found at a blog named New Think Tank.

    The login scripts along with MySQL handle the login and authentication. Once you get it working the download part is easy.
    Each day is a learning experience.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •