Reply
PHP password protect file script
Old 03-25-2006, 12:47 PM PHP password protect file script
wirlwind1's Avatar
Junior Talker

Posts: 2
I thought I share this script I found on the net. If you need to password protect a file in your directory here it is:

Copy and save this code as password.php or etc.

Code:
<?php

session_start();

$admin_user_name = "admin";
$admin_password = "pass";
//you can change the username and password by changing the above two strings 

if (!isset($HTTP_SESSION_VARS['user'])) {
    
    if(isset($HTTP_POST_VARS['u_name'])) 
        $u_name = $HTTP_POST_VARS['u_name'];
    
    if(isset($HTTP_POST_VARS['u_password'])) 
        $u_password = $HTTP_POST_VARS['u_password'];
    
    if(!isset($u_name)) {
        ?>
        <HTML>
        <HEAD>
        <TITLE><?php echo $HTTP_SERVER_VARS['HTTP_HOST']; ?> : Authentication Required</TITLE>
        </HEAD>
        <BODY bgcolor=#ffffff>
        <table border=0 cellspacing=0 cellpadding=0 width=100%>
             <TR><TD>
             <font face=verdana size=2><B>(Access Restricted to Authorized Personnel)</b> </font></td>
             </tr></table>
        <P></P>
        <font face=verdana size=2>
        <center>
        <?php
        $form_to = "http://$HTTP_SERVER_VARS[HTTP_HOST]$HTTP_SERVER_VARS[PHP_SELF]";
        
        if(isset($HTTP_SERVER_VARS["QUERY_STRING"]))
        $form_to = $form_to ."?". $HTTP_SERVER_VARS["QUERY_STRING"];
        
        ?>
        <form method=post action=<?php echo $form_to; ?>>
        <table border=0 width=350>
        <TR>
        <TD><font face=verdana size=2><B>User Name</B></font></TD>
        <TD><font face=verdana size=2><input type=text name=u_name size=20></font></TD></TR>
        <TR>
        <TD><font face=verdana size=2><B>Password</B></font></TD>
        <TD><font face=verdana size=2><input type=password name=u_password size=20></font></TD>
        </TR>
        </table>
        <input type=submit value=Login></form>
        </center>
        </font>
        </BODY>
        </HTML>
        
        <?php
        exit;
    }
    else {
        
        function login_error($host,$php_self) {
            echo "<HTML><HEAD>
            <TITLE>$host :  Administration</TITLE>
            </HEAD><BODY bgcolor=#ffffff>
            <table border=0 cellspacing=0 cellpadding=0 width=100%>
                 <TR><TD align=left>
                 <font face=verdana size=2><B> &nbsp;You Need to log on to access this part of the site! </b> </font></td>
                 </tr></table>
            <P></P>
            <font face=verdana size=2>
            <center>";
                        
            echo "Error: You are not authorized to access this part of the site!
            <B><a href=$php_self>Click here</a></b> to login again.<P>
            </center>
            </font>
            </BODY>
            </HTML>";
            session_unregister("adb_password");
            session_unregister("user");
            exit;
        }
        
        $user_checked_passed = false;
        
        
        if(isset($HTTP_SESSION_VARS['adb_password'])) {
            
            $adb_session_password = $HTTP_SESSION_VARS['adb_password'];
            
            if($admin_password != $adb_session_password) 
                login_error($HTTP_SERVER_VARS['HTTP_HOST'],$HTTP_SERVER_VARS['PHP_SELF']);
            else {
                $user_checked_passed = true;
            }
        }
        
        
        if($user_checked_passed == false) {
            
            if(strlen($u_name)< 2) 
                login_error($HTTP_SERVER_VARS['HTTP_HOST'],$HTTP_SERVER_VARS['PHP_SELF']);
            
            if($admin_user_name != $u_name) //if username not correct
                login_error($HTTP_SERVER_VARS['HTTP_HOST'],$HTTP_SERVER_VARS['PHP_SELF']);        
            
            if(isset($admin_password)) {
                
                if($admin_password == $u_password) {
                    
                    session_register("adb_password");
                    session_register("user");
                    
                    $adb_password = $admin_password;
                    $user = $u_name;
                }
                else { //password in-correct
                    login_error($HTTP_SERVER_VARS['HTTP_HOST'],$HTTP_SERVER_VARS['PHP_SELF']);
                }
            }
            else {
                login_error($HTTP_SERVER_VARS['HTTP_HOST'],$HTTP_SERVER_VARS['PHP_SELF']);
            }
                
            $page_location = $HTTP_SERVER_VARS['PHP_SELF'];
            if(isset($HTTP_SERVER_VARS["QUERY_STRING"]))
            $page_location = $page_location ."?". $HTTP_SERVER_VARS["QUERY_STRING"];
            
            header ("Location: ". $page_location);
        }
    }
}
?>
then <include that file above> on the very top of the (index.php) or any other file you may need to protect from.

Let me know if you need help.
wirlwind1 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-20-2006, 07:06 PM
Junior Talker

Posts: 23
Thats the same exact password thing I use
oxymoron is offline
Reply With Quote
View Public Profile
 
Old 05-12-2006, 03:37 PM
Junior Talker

Posts: 20
Thank you for posting this script. I have all these files on my webserver, and barely any protection for them.
Jasix is offline
Reply With Quote
View Public Profile
 
Old 05-13-2006, 02:50 AM
Junior Talker

Posts: 0
For those who don't know any php, the syntax for the include command is:

include('FILENAMEHERE.php');
thrawn is offline
Reply With Quote
View Public Profile
 
Old 05-28-2006, 10:19 AM
Experienced Talker

Posts: 40
Quote:
Originally Posted by Jasix
Thank you for posting this script. I have all these files on my webserver, and barely any protection for them.
No problem I hope you post up some of yours if any. Thanks
Ikonic is offline
Reply With Quote
View Public Profile
 
Old 07-18-2006, 04:38 PM Great! suggestions please.
Junior Talker

Posts: 1
Great script. Do you have any suggestions for my site? Just take a look at it please. I made it part of my signature.
searchrentals12 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP password protect file script
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12696 seconds with 12 queries