By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

Password protect a file

P: 60
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
May 29 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
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.,:

Expand|Select|Wrap|Line Numbers
  1. if(checkLogin($_POST['username'], $_POST['password'])
  2.     readfile('/path/to/sensitive/document.ext');
  3. else
  4.     print('Invalid Info');
  5.  
http://php.net/readfile

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.]
May 29 '07 #2

P: 60
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 ???
May 29 '07 #3

pbmods
Expert 5K+
P: 5,821
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.
May 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.