#native_company# #native_desc#
#native_cta#

Htpasswd class wrapper to add/delete/modify users in .htpass

By alman
on December 1, 2001

Version: 1.1

Type: Full Script

Category: File Management

License: GNU General Public License

Description: I have used the class.Htpasswd.php3 from http://www.thewebmasters.net/php/Htpasswd.phtml and written a very simple wrapper PHP script around it. Check the following things… 1) The script itself needs to be protected by proper .htaccess mechanism so that only authorized users can play with it and 2) The .htpasswd file needs to be writeable by httpd (nobody) Hope you find this useful.. AMit

<?php
// A Very rudimentary PHP htpasswd user admin
// Copyright Amit Chakradeo 2000
// Download the latest version from http://thewebmasters.net/
include("class.Htpasswd.php"); 

/* Change this variable to suit your .htpasswd file
  For most users, this file would be either 
  /home/yourname/.htpasswd		OR
  /home2/yourname/.htpasswd

*/

$htpasswdfile = "/home/httpd/files/myhtpasswordfile";
$Htpasswd = new Htpasswd($htpasswdfile);

// Do not change anything below this line...
//if(!isset($operation)) {
   // We are not under form submission, display the form:
echo '<FORM ACTION="'.$PHP_SELF . '" METHOD=POST>';
?> 
<Table BORDER><TR>
<TD>Username: </TD>
<TD><INPUT TYPE=text name="username"></TD>
</TR><TR>
<TD>Password: </TD>
<TD><INPUT TYPE=text name="password"></TD>
</TR><TR>
<TD>
<INPUT TYPE=radio NAME=doop VALUE="verify" CHECKED> Verify Password <BR>
<INPUT TYPE=radio NAME=doop VALUE="add" > Add New User <BR>
<INPUT TYPE=radio NAME=doop VALUE="delete" > Delete specified user <BR>
<INPUT TYPE=radio NAME=doop VALUE="change" > Change Password <BR>
</TD>
<TD>
<INPUT TYPE=submit name="operation">
</TD></TR>
</TR>
</TABLE>
</FORM>
// added by alman
<?
$Htpasswd->htReadFile();
 if ($Htpasswd->USERCOUNT>0){
?>
<table width="50%" border="1" cellspacing="0" cellpadding="0">
  <tr> 
    <td>Below are the users in the password file</td>
  </tr>
  <tr>
    <td>
      <?
    for ($i=0;$i<$Htpasswd->USERCOUNT;$i++){
			echo "	<tr>n";
			echo "	<td>&nbsp;".$Htpasswd->USERS[$i][ "user"]."</td>n";
			echo "	</tr>n";
    }
  echo "</table>";
  }
  else echo "No users in the password access file.";
?>
    </td>
  </tr>
</table>
<?php 
if(!isset($operation)) {
}
else {
echo "<FONT COLOR=RED> <HR>";
echo "The result of the operation you just did <BR><HR>";
echo "You want to do operation = [$doop] on user $username with password $password <BR>";
$handle = new Htpasswd($htpasswdfile);

switch ($doop) {
	case 'add':
		print "Code to add user <BR>";
		$retval = $handle->addUser($username, $password);
  		if($retval) {
			print "User [$username] added succefully<BR>";
		} else {
			print "Error while adding new user [$username]<BR>";
		}
		break;

	case 'change':
		print 'Code to change password <BR>';
		$retval = $handle->changePass($username, $password);
		if($retval) {
         print "Password for [$username] changed succefully<BR>";
      } else {
         print "Error while changing password for [$username] (Doesnt exist probably!)<BR>";
      }
		break;

	case 'delete':
		print 'Code to delete user <BR>';
      $retval = $handle->deleteUser($username);
      if($retval) {
         print "[$username] deleted<BR>";
      } else {
         print "Error while deleting [$username], perhaps he wasn't there!<BR>";
      }
		break;

	case 'verify':
		print 'Code to verify user's password<BR>';	
		$retval = $handle->verifyUser($username, $password);
      if($retval) {
         print "[$username] verifies okay with password [$password]<BR>";
      } else {
         print "Verify Error for [$username] with [$password] (wrong password or user doesn't exist!) <BR>";
      }

		break;

	default:
		print 'Why did you break my heart ? <BR>';
		}
echo "</FONT>";
}
?>