Password protect a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pplers
    New Member
    • Apr 2007
    • 60

    Password protect a file

    Hi,

    i would like to know how can i password protect a file. In my case, i want to protect a log, i tried using a form but i don't know how to make it do nothing (just show the file contents) if the pass is correct.

    p.s: i want an alternative to .htaccess



    thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    If you're trying to view an external file, your best bet would be to set up a conditional that will only output the file if the User provides valid credentials.

    E.g.,:

    [code=php]
    if(checkLogin($ _POST['username'], $_POST['password'])
    readfile('/path/to/sensitive/document.ext');
    else
    print('Invalid Info');
    [/code]



    This is a very basic example, but it should be enough to point you in the right direction.

    [EDIT: For added security, store the file you're accessing OUTSIDE your webserver's document root.]

    Comment

    • pplers
      New Member
      • Apr 2007
      • 60

      #3
      I have up.php and log.php , up.php writes in log.php
      What i want is to protect the content of log.php without .htaccess

      So i can't do that.


      Any other way ???

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by pplers
        I have up.php and log.php , up.php writes in log.php
        What i want is to protect the content of log.php without .htaccess
        Which does not contradict what I suggested in my previous post.

        Keep in mind that php can read/write to files that are outside of your webserver's document root. All you have to do is move your log.php file outside of your web root so that browsers can't directly access it, then use the script mentioned in my last post to selectively allow access.

        Note that 'checkLogin' refers to whatever function you have defined to check the User's login info.

        Comment

        Working...