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

RSS Twitter

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

by: alman
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 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

// A Very rudimentary PHP htpasswd user admin
// Copyright Amit Chakradeo 2000
// Download the latest version from

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


$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:
<Table BORDER><TR>
<TD>Username: </TD>
<TD><INPUT TYPE=text name="username"></TD>
<TD>Password: </TD>
<TD><INPUT TYPE=text name="password"></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>
<INPUT TYPE=submit name="operation">
// added by alman
 if ($Htpasswd->USERCOUNT>0){
<table width="50%" border="1" cellspacing="0" cellpadding="0">
    <td>Below are the users in the password file</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.";
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>";

	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>";

	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>";

	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>";


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

Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.