Search code examples
phpfilepasswordsprotected

How Can I write to a file in a password protected directory?


I am creating an application form, here is what it does.

The php script takes the form information and writes a file named usr_firstname_lastname.txt

The directory is password protected using .htaccess and .thpasswd

.htaccess

# DO NOT REMOVE THIS LINE AND THE LINES BELOW PWPROTECTID:******
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /home/a9368227/public_html/applications/.htpasswd
Require user *****
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE ******:PWPROTECTID

.htpasswd

*****:*********************************/

The '*' are obviously to prevent peope obtaining login info, however this is just to tell you what the the format is.

Now the point of password protecting the directory is so that only people with the password can see it, however I want it to be accessable. The only answers I can find on this are chmodding the directory to 711, I want everyone to be able to read the files, just need a password first, I don't want to block them.

I do not want to change the directory, I want all applications to be easily accessable by typing/applications at the end of the domain and entering a name. And for my purposes the directory must be password protected and chmodded to 755.

I'd think there would be a simpe answer on how to like pass a password and username to the directory, can anyone help?

If there is no way, I should be able to make a simple PHP script that asks for username and password and then lists the applications below, but I'd rather not...


Solution

  • The .htaccess and .htpasswd file control access from the Internet directly to the directory. The chmod permissions control access from your PHP file to that directory in the filesystem. The two are not related. You can chmod it to 755 so that your PHP script can access it, and still put password protection on the directory if it is accessed directly from the Internet.