Hi everyoen
I am writing an access system and it needs to be able to encrypt the
username and password to a certain directory's .htpasswd file. I'm
not sure how to do it. I found a link with this code:
// The following class encrypts a password, and writes it to a .htpasswd
// file for use with .htaccess encryption.
// Example usage:
//
// $username="bob";
// $password = "bob";
// $theLine = $htpasswd->genLine($username, $password);
// $htpasswd->writeFile(".htpasswd",$theLine);
//
//
//
// NOTE: Do NOT encrypt the password before calling the genLine function,
// otherwise the password will be encrypted twice, and will not work.
//
// Any questions, IM me www NLH com on AIM. Enjoy! =)
// START CODE
class htpasswd {
// Encrypts given password
function encryptPW($thePW){
$thePW = crypt(trim($thePW),base64_encode(CRYPT_STD_DES));
return $thePW;
}
// Calls the encryptPW function, generates the line for writing
function genLine($username,$password){
$encrypted_password = encryptPW($password);
return "$username:$encrypted_password";
}
// Writes data to the file
function writeFile($theFile,$theLine){
$fp = fopen($theFile, "a");
$orig_size = filesize($theFile);
// Trims all whitespace
$theContents = file_get_contents($theFile);
$strippedContents = str_replace(" ", "", $theContents);
ftruncate($fp, 0);
fwrite($fp, $strippedContents);
// Sets file pointer to the end of the file
fseek($fp, filesize($theFile));
// If this is the first entry in the file, do not add a new
line before writing
if($orig_size == 1){
fwrite($fp, "$theLine");
}else{
// If this is not the first entry, write the data on a new
line in the file
fwrite($fp, "$theLine");
}
fclose($fp);
}
}
// END CODE
But I tried the code and got a weird error that didn't make any sense.
Can someone help shed some light so I can get this thing written
properly?