Jump to content

Password protect a directory with PHP


AndrewJ1313

Recommended Posts

I have a section of a web site password protected via htaccess. The issue I am having is, I want to be able to have the user log in using an HTML form validating the user from a database of about 400 users instead of the default pop up for htaccess. I do not have access to directly alter code within the htaccess file as it is only controlled through a cPanel interface.

 

Is there a work around to this beside passing "username:password@domain.com" ? From what I have read on google, this function doesn't even work in IE anymore and in Firefox, it still pops up with an alert message "You are about to log in to 'directory' using Username. Continue?"

 

Is there a way to password protect a directory with just PHP? Protecting .php files is not the issue, it's various PDF and image files I need to protect.

 

Thanks for any insight.

Andrew

Link to comment
Share on other sites

It sounds like you need to put the files into a folder that only php has access to (i.e. either put the folder outside your document root folder or deny all http requests to the files in the folder) and then dynamically output the files using a .php script. The .php script then checks if the current visitor is logged in before outputting the correct content type header followed by the contents of the correct file (assuming you want the file to open in the browser) and/or perform a 'force download', where the download dialog box is presented to allow the visitor to download and save the file on their system.

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.