Jump to content

php editing .htpasswd


seularts

Recommended Posts

I have found this small script.. but it has an issue.. i can't replace a user if i want.. it just adds another line with the user and a different password.. can anyone give me a hint!?

 

<?php

 

$AllowAddNewUser = "TRUE";

$htpasswd = "editht/.htpasswd";

 

if (isset($_POST['submit']))

 

{

function generateString($user,$pass)

{

 

$saltchars = array(

 

'a','b','c','d','e','f','g','h','i','j','k','l','m',

'n','o','p','q','r','s','t','u','v','w','x','y','z',

'A','B','C','D','E','F','G','H','I','J','K','L','M',

'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',

'1','2','3','4','5','6','7','8','9','0','.','/'

);

srand((double)microtime()*1000000);

$saltcount = "2";

$des_salt = '';

for ($i=0; $i<$saltcount; $i++)

{

$des_salt .= $saltchars[rand(0,count($saltchars))];

}

 

$encrypted = crypt($pass,$des_salt);

$return = $user . ":" . $encrypted . "\n";

return $return;

}

 

if ($_POST['p'] != $_POST['conf_p'])

{

echo "Passwords do not match. Killing script...<br /><br />";

die;

}

 

$Fp = fopen($htpasswd, "r");

$Contents = fread($Fp, filesize($htpasswd));

fclose($Fp);

$String = explode("\n", $Contents);

 

for ($i=0; $i<count($String); $i++)

 

{

$chkString = explode(":", $String[$i]);

if($chkString[0] == $_POST['u'])

$newContents = '';

{

$Str[$i] = $String[$i] . "\n\n";

$newContents = str_replace($Str[$i], "", $Contents);

}

}

 

$InsContents = $newContents;

if (!$newContents)

{

if (isset($_POST['AddNew']))

{

echo "Could not locate specified user so creating new account<br /><br />";

$InsContents = $Contents;

} else

 

{

echo "User could not be found. Killing script...<br /><br />";

die;

}

}

 

 

$insString = generateString($_POST['u'], $_POST['p']); // Generate the user:pass string

$newHtpasswd = $InsContents . $insString; // Set the new contents of the file

$Handle = fopen($htpasswd, "w");

if (!fputs($Handle, $newHtpasswd))

{

echo "Could not write new passwd file";

} else

{

echo "New passwd file written successfully";

}

fclose($Handle);

} else

{

echo "<form name=\"changePass\" action=\"$PHP_SELF\" method=\"POST\">

<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" bgcolor=\"#DDDDDD\">

<tr><td bgcolor=\"#FFFFFF\">Username:</td><td bgcolor=\"#FFFFFF\"><input type=\"text\" name=\"u\" /></td></tr>

<tr><td bgcolor=\"#FFFFFF\">New Password:</td><td bgcolor=\"#FFFFFF\"><input type=\"password\" name=\"p\" /></td

></tr>

<tr><td bgcolor=\"#FFFFFF\">Confirm New<br />Password:</td><td bgcolor=\"#FFFFFF\"><input type=\"password\" name

=\"conf_p\" /></td></tr>";

 

if ($AllowAddNewUser == "TRUE") // If the variable is TRUE then show the checkbox

{

echo "<tr><td bgcolor=\"#FFFFFF\">Create new user<br />if this user does<br />not exist?</td><td bgcolor=\"#

FFFFFF\"><input type=\"checkbox\" name=\"AddNew\"></td></tr>";

}

echo "<tr><td bgcolor=\"#FFFFFF\" colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"

Change Password\"></td></tr>

</table>

</form>";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/96460-php-editing-htpasswd/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.