#native_company# #native_desc#

htpasswd manager

By Loren
on June 13, 2002

Version: .9

Type: Sample Code (HOWTO)

Category: File Management

License: GNU General Public License

Description: allows an admin to alter the .htpasswd from a webpage. I was useing apache 1.3 on a Windows 98.

<TITLE>Edit Password</TITLE>

NOTE: this code is in beta stages, 
it requires that the htpasswd.exe 
and the .htpasswd is in the same 
directory as the PHP file.   This is 
the best I could do under windows98
please help [email protected]
wrote a program that will spit out
a file with ".htpasswd" as a file 
name if that is a problem.  E-mail 
me if needed.

/* This function returns the position of string s1 within string s2.
   The position is 1 based.  If s1 is not in s2, 0 is returned.
   function code was taken from jeremycreed on phpbuilder.com
function InStr($s1, $s2)
        //Check for valid input
        if(!(is_string($s1) && is_string($s2))) return 0;

        $s1len = strlen($s1);
        $s2len = strlen($s2);

        //Check if s1 in s2 at all
        if(!ereg($s1, $s2)) return 0;

        //Resolve simple case
        if($s1 == $s2) return 1;

        //Set initial search limits
        $begin = 0;
        $end = $s2len - $s1len;

        //Initialize position
        $position = 0;

        //Do binary search of s2 for s1
        //Check left side first to find first occurance of s1
        //Check right side first to find last occurance of s1
        while($end > $begin + 1)
                $middle = ceil(($begin + $end) / 2);
                $leftBegin = $begin;
                $rightBegin = $middle + $s1len;
                $leftEnd = $middle;
                $rightEnd = $end + $s1len;

                //Check left first
                if(ereg($s1, substr($s2, $leftBegin, $rightBegin - $leftBegin)))
                        $end = $middle;
                else //(ereg($s1, substr($s2, $leftEnd, $rightEnd - $leftEnd)))
                        $position += $middle - $begin;
                        $begin = $middle;

        //Resolve 1 off problems introduced by ceil
        if(ereg($s1, substr($s2, $end, $s1len))) $position++;

        //Return position 1 based
        return $position + 1;


<form action="<?$PHP_SELF?>" method="post">

<B>Admin Password: </b><input type="text" name="AdminPass" size="20"><br><br>

<b>User: </b><input type="text" name="User" size="20"><br>
<b>User Pass: </b><input type = "text" name ="UserPW" size="20"><br>

<b>Add/Edit</b><input type="radio" name="add" value="add">
<b>Remove</b><input type="radio" name="remove" Value="remove">

<input type="submit" value="Go">

if($AdminPass == "ADMIN" && isset($add))    //set ADMIN to the password you would like
     exec("htpasswd -b .htpasswd $User $UserPW");
     echo "Password for $User has been added";

elseif($AdminPass == "tacotaco" && isset($remove))

 $FileOpen = fopen("C:/website/pass/" . ".htpasswd", "r");               //chage C:/website/pass/ to the directory where the .htpasswd is located.
 $htPW = fread($FileOpen, filesize("C:/website/pass/" . ".htpasswd"));

 if(InStr($User, $htPW))
      $Len = (strlen($User) + 39);
      $Pos = InStr($User, $htPW) - 1;
      $NewPW = substr($htPW, 0, $Pos) . substr($htPW, ($Pos + $Len), strlen($htPW)); 
      $FileOpen = fopen("C:/website/pass/" . ".htpasswd", "w");
       fwrite($FileOpen, $NewPW);
       echo "User $User Removed";
     echo "User Not Found!";