carbomite Posted December 23, 2010 Share Posted December 23, 2010 Hi, I am trying to create .htaccess and .htpasswd files in PHP using fopen and fwrite. But I don't think I'm encrypting the password correctly for the .htpasswd file. When I enter the username and password on the protected page, I am asked to enter them again. My error logs say "Password Mismatch". I have tried encrypting with: $encrypwd = crypt($pwd); and $encrypwd = md5($pwd); my username, password and create .htpasswd code $name = "6"; $pwd = "6"; $hpfile = $name."/private/.htpasswd"; $fh2 = fopen($hpfile, 'w') or die("can't open file"); $stringData = $name.":".$encrypwd; fwrite($fh2, $stringData); fclose($fh2); When I create a .htpasswd manually (entering the username and using an online htpasswd generator for the encrypted password) I am successful. I am using Xampp Lite, Windows 7, PHP Version 5.3.1 In case it matters Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted December 23, 2010 Share Posted December 23, 2010 Have you looked at the .htpasswd file? It might be malformed or something. I see nothing wrong with your code. Quote Link to comment Share on other sites More sharing options...
carbomite Posted December 23, 2010 Author Share Posted December 23, 2010 when using $encrypwd = md5($pwd); the .htpasswd file has "6:1679091c5a880faf6fb5e6087eb1b2dc" when using $encrypwd = crypt($pwd); the .htpasswd file has "6:$1$5V0.oV5.$1nmiJJQI5hEcgpfBVwPqa0" these give me the "Password Mismatch" error in the logs but when using an online generator (http://www.htaccesstools.com/htpasswd-generator) I put "6:$apr1$Og2ky/..$0Vs5LvxD54.f3MBEufzNE0" in the .htpasswd file and it works as expected when I log in Quote Link to comment Share on other sites More sharing options...
trq Posted December 23, 2010 Share Posted December 23, 2010 Are you using the same passwords in all these examples? All that online editor does is md5 hashes the passwords. Quote Link to comment Share on other sites More sharing options...
carbomite Posted December 23, 2010 Author Share Posted December 23, 2010 yea, I used the username/password of 6/6 and I tried it again with moon/moon it still doesn't work with md5() or crypt() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.