Php updating a .htpasswd file!?

Can anyone help me with a script for php to update a .htpasswd file with inserting a user:password per line, making the password encrypted!?

What have you got so far?

The htaccess and the htpasswd… but i have no idea how to make a php to input data into the htpasswd !?

You might want to check out this page on reading from and writing to remote files.

I might add that i am skiddish with php… i was looking for a Form with the php script attached to it perhapes if i am lucky to edit the .htpasswd file:D

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!?

[code]<?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…

”;
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] . “nn”;
$newContents = str_replace($Str[$i], “”, $Contents);
}
}

$InsContents = $newContents;
if (!$newContents)
{
if (isset($_POST[‘AddNew’]))
{
echo “Could not locate specified user so creating new account

”;
$InsContents = $Contents;
} else

{
echo “User could not be found. Killing script…

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

";

if ($AllowAddNewUser == “TRUE”) // If the variable is TRUE then show the checkbox
{
echo “

”;
}
echo "
Username:
New Password:
Confirm New
Password:
Create new user
if this user does
not exist?
"; } ?>[/code]

MOD EDIT: Added code tags.

Sorry, we don’t provide support on ‘found’ scripts, only on created scripts. This is because there’s someone else out there who has more knowledge of the script you found.

What you need to do is read the full contents of the file you’re trying to edit, find the parts that you want to change, change them, truncate the file and then write the changed contents towards it.

Sponsor our Newsletter | Privacy Policy | Terms of Service