Password Protect 1 index.php file only

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
james64
Forum Newbie
Posts: 5
Joined: Tue Nov 04, 2008 3:39 pm

Password Protect 1 index.php file only

Post by james64 »

I have been looking all over for a resolution to this problem, but I can't seem to find an answer anywhere...

What I want to do is password protect only 1 index.php file on my server, which resides in the home directory. Every other index.php that resides in other directories should be publicly accessible.

I tried using an .htaccess/.htpasswd combination with the following in my .htaccess file:

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /directory/to/.htpasswd
AuthGroupFile /dev/null
<Files index.php>
require valid-user
</Files>

Unfortunately, this code is password protecting every single index.php file on my server. In addition, I have also tried using the password protect script at http://www.zubrag.com/scripts/password-protect.php, but unfortunately, this seems to password protect my index.html file as well in the home directory, which I can't have.

I have also tried creating a condition in the password-protect.php file from the above site (if $_SERVER['SCRIPT_NAME'] == '/index.html') { exit(); } else { rest of script goes here; }), but no matter what I do, it either password protects both my index.html and index.php in my home directory or neither of them.

Also tried scouring these boards, but couldn't find an answer to this specific problem.

If anyone knows how to only password protect the index.php file in my home directory only and no other file (including other index.php files in other directories), I would greatly appreciate it. Thank you.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Password Protect 1 index.php file only

Post by JAB Creations »

Code: Select all

<form action="" method="post"><input name="post_pass" type="password" value="" /><input type="submit" /></form>

Code: Select all

<?php
if ($_POST['post_pass'] == 'this_is_the_password') {echo 'here is content no one else should see'}
else {echo 'go away or I shall taunt you a second time!';}
?>
james64
Forum Newbie
Posts: 5
Joined: Tue Nov 04, 2008 3:39 pm

Re: Password Protect 1 index.php file only

Post by james64 »

Thanks for the reply JAB, but that is also requiring a password when I try to access index.html (even though I put the code in my index.php file). The code you provided me is very similar to the password_protect.php script I talked about in my first post.

Hopefully this will make things easier: Instead of just only password protecting the index.php file in my home directory, is there a way I can just bypass the password authentication being done by the .htaccess?

While I have multiple index.php files in various directories, the only one that really must be open to the public is the one I have in my /root/forum directory. If it matters, the .htaccess is in the root directory.

Honestly, the .htaccess is working exactly the way I want it to; I just need my forum to be accessible to the public as every time someone registers for an account, it brings them to the Authorization Required error page from .htaccess. If people see that, I know they will panic so if I can just make this directory open to the public, I know it will work the way it should.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Password Protect 1 index.php file only

Post by JAB Creations »

You don't even seem to know what you want to do so let's make this dead simple...

Apache - Will project all the files in the directory period.

PHP - Will project all the files that you apply the script to.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Password Protect 1 index.php file only

Post by Eran »


<Files index.php>
require valid-user
</Files>
You are applying it to all index.php files. Try to specify a directory, either an absolute or relative:

Code: Select all

 
<Files ./index.php>
Or
<Files /absolute/path/to/index.php>
 
james64
Forum Newbie
Posts: 5
Joined: Tue Nov 04, 2008 3:39 pm

Re: Password Protect 1 index.php file only

Post by james64 »

Alright, I know this was kind of a roller-coaster of a question, but I figured it out. It seemed that my server was calling index.php before index.html, so what I did to resolve the problem is I added the following entry in my .htaccess in my home directory:

Code: Select all

DirectoryIndex index.html index.php
This allows my index.html file to load before index.php and now that is the case, I can do what I was trying to do. Thanks for the help guys.
Post Reply