Hi Marian, $thePW = crypt(trim($thePW),base64_encode(CRYPT_STD_DES)); This line encrypts the password to the usual 13 char string that online htpasswd generators give you.
Also, it would be better to check things like PW length and whether the PW has numeric and alpha etc. then warn that the password is not good enough. Have you created a password file, which would be located above root? If these passwords are generated online, you would need a secure connection. Depends on what you're protecting. I'm presuming you know what to put in the .htaccess file. Regards, Bob Exton. ----- Original Message ----- From: "Marian Briones" <[EMAIL PROTECTED]> To: <[email protected]> Sent: Wednesday, April 04, 2007 9:35 PM Subject: [php-list] Writing to a .htpasswd file > 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? Community email addresses: Post message: [email protected] Subscribe: [EMAIL PROTECTED] Unsubscribe: [EMAIL PROTECTED] List owner: [EMAIL PROTECTED] Shortcut URL to this page: http://groups.yahoo.com/group/php-list Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/php-list/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/php-list/join (Yahoo! ID required) <*> To change settings via email: mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
