Forum Moderators: coopster
All of this is done in php and works fine. The problem arises when they log out and then want to login again. Its like the logout function ruins the password. How - i dont know
I dont have a clue about what is wrong. Do any of you have suggestions of possible errors or just where to start looking?
Thanks a lot
The pasword is stored in a mySQL database. The password is however encrypted in the database so I cant look up the exact password.
But I can see that the password is stored in the database after the initial registration. And it is also possible to login once.
My only guess the logout function. It is:
<?php
unset($_SESSION['company_profile_id'], $_SESSION['company_profile_name']);
header("Location: /");
?>
But aint it a very normal one?
Thanks in advance
Can I assume you've gone step by step as a new registered user, checking the data stored as you go? In other words, perform the registration, and do a query on the database for the new login data. Once you can verify the login data, logout and see if anything in the database changes from the initial login.
Another thought; you mentioned the password is 'encrypted', are you referring to a one-way hash such as MD5 or SHA1? If so, are you checking the hashed password in the login against the value stored in the database? In other words, after you store the hash, you make sure to re-hash the login data to compare it against?
In keeping with that line of thought, have you bothered to take a string, run the same hash algorithm against it, and compare it to what is stored in the database `password` field?
One last thought related to the password; what column data type / length did you use? Be sure the hashed password string isn't being truncated because the field is too short.
If I've made the wrong assumption and you're using some other method to store the password, e.g. MySQL's PASSWORD function or PHP's crypt() function, then you should strongly reconsider and use a better hash method such as SHA256. My personal feeling, most applications are just fine with SHA1 and a salt, especially because SHA1 is so portable - it's just available across so many languages. MySQL has a built in SHA1 function, and there are some good JavaScript implementations as well.
you may also create (just for test purposes) a mini script to echo any md5(PW)
and again check if what you got is what is expected
error often occurs from double md5()
However - if the user tries to change the password it is still encrypted.
I had a friend to help me with the code cus im still very new to PHP so I cant figure out the problem.
Here is the code:
This code is a part of the "change password form"
<?php
if($_POST["password1"] == $_POST["password2"]){
//print_r($_SESSION);
//echo $_SESSION["name"]."<br>".$_POST["password1"];
$password_enc = Utils::encrypt($_SESSION['name'],$_POST['password1']);
$data = array(
'password' => "'".$password_enc."'"
);
$db = $GLOBALS["db"];
$n = $db->query('UPDATE company_profile SET password = ? WHERE id = ?', array($password_enc, $_SESSION["company_profile_id"]));
echo "Password updated";
} else {
echo "password mismatch";
}
?>
The "Utils::encrypt" comes from this code:
public static function encrypt($username,$password){
$salt = self::generateSalt($username);
$encrypted = base64_encode(sha1($salt.$password,true));
return $encrypted;
}
private static function generateSalt($username){
$step = floor(strlen($username) / 2 + 1);
$username = md5($username);
$i = 0;
$salt = "";
while(strlen($salt) < 10){
$salt .= $username{$i};
$i = ($i + $step) % strlen($username);
}
return $salt;
}
The field for the password in the mySQL database is a varchar(255) so there should be plenty of space.
There is no change in the database when the user log in and out (ive checked) so as far as I can see - it must be the Utils::encrypt function that messes things up.
You said:
"Another thought; you mentioned the password is 'encrypted', are you referring to a one-way hash such as MD5 or SHA1? If so, are you checking the hashed password in the login against the value stored in the database? In other words, after you store the hash, you make sure to re-hash the login data to compare it against?"
I dont really get this part. How do you re-hash?
Sorry if I sound very stupid but I think this is somewhat more difficult than I assumed.
Normally I can do what I need in PHP but its far from my strongest side :-)
So in order to log a user in, you need to reproduce this process in part, by 're-hashing' the plain password value the user inputs.