sungpeng Posted November 16, 2011 Share Posted November 16, 2011 Why is it always unsuccessful? <?php $password = CRYPT('mypassword'); if (CRYPT($user_input, $password) == $password) { echo "Password verified!"; }else{echo "unsuccessful";} ?> Link to comment https://forums.phpfreaks.com/topic/251232-crypt-error/ Share on other sites More sharing options...
phporcaffeine Posted November 16, 2011 Share Posted November 16, 2011 When using crypt, you should always use a salt. Using crypt() without a salt can produce varying and inconsistent results. Also, while function but as a matter of practice, you should reference baked-in constructs in lowercase form ( crypt() Vs. CRYPT() ), unless otherwise noted by the PHP manual. Try this: <?php $salt = "asdfghjkl"; $password = crypt('mypassword', $salt); if (crypt($user_input, $salt) == $password) { echo "Password verified!"; } else { echo "unsuccessful"; } ?> BTW: While fine for learning, I would not consider the above as a login method for a production system. There are better, more secure ways of doing authentication. Link to comment https://forums.phpfreaks.com/topic/251232-crypt-error/#findComment-1288586 Share on other sites More sharing options...
sungpeng Posted November 16, 2011 Author Share Posted November 16, 2011 <?php $salt = "asdfghjkl"; $password = CRYPT('mypassword', $salt); if (CRYPT($user_input, $salt) == $password) { echo "Password verified!"; } else { echo "unsuccessful"; } ?> Still unsuccessful? Link to comment https://forums.phpfreaks.com/topic/251232-crypt-error/#findComment-1288589 Share on other sites More sharing options...
MasterACE14 Posted November 16, 2011 Share Posted November 16, 2011 does $user_input also contain 'mypassword' ? Link to comment https://forums.phpfreaks.com/topic/251232-crypt-error/#findComment-1288590 Share on other sites More sharing options...
phporcaffeine Posted November 16, 2011 Share Posted November 16, 2011 This works just fine: <?php $salt = "asdfghjkl"; $user_input = 'mypassword'; $password = crypt('mypassword', $salt); if (crypt($user_input, $salt) == $password) { echo "Password verified!"; } else { echo "unsuccessful"; } ?> Notice that I specifically set $user_input. So if it didn't work for you, it would indicate that you're not setting the $user_input variable. Link to comment https://forums.phpfreaks.com/topic/251232-crypt-error/#findComment-1288591 Share on other sites More sharing options...
sungpeng Posted November 16, 2011 Author Share Posted November 16, 2011 Thank phporcaffeine Link to comment https://forums.phpfreaks.com/topic/251232-crypt-error/#findComment-1288594 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.