1

I am creating my change password site for my website and I have some problems with the code...

For some reason i have difficulties with the passwords being compared and replaced in the db after crypting them.

I wanted this:

Either get the current users password and compare it to the input value of $oldpass or compare the input value of $oldpass with the password stored in the database for the current user.

After checking if the $oldpass and the password from the database match and IF they match then take the input value of $newpass and $repeatpass, compare them and if they match, then crypt() $newpass and update the database with the new password.

I am not even sure if the passwords are even crypted.

Also in the code I am comparing $oldpass with $_SESSION['password'] which is not the password from the db, I can't figure out how to call the password from the db.

    <?php

include 'check_login_status.php';

$u="";
$oldpass=md5($_POST['oldpass']);
//stripping both strings of white spaces
$newpass = preg_replace('#[^a-z0-9]#i', '', $_POST['newpass']);
$repeatpass = preg_replace('#[^a-z0-9]#i', '', $_POST['repeatpass']);

//get the username from the header
if(isset($_GET["u"])){
    $u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
} else {
    header("location: compare_pass.php?u=".$_SESSION["username"]);
    exit(); 
}

// Select the member from the users table
$sql = "SELECT password FROM users WHERE username='$u' LIMIT 1";
mysqli_query($db_conx, $sql);
$user_query = mysqli_query($db_conx, $sql);
// Now make sure that user exists in the table
$numrows = mysqli_num_rows($user_query);
if($numrows < 1){
    echo "That user does not exist or is not yet activated, press back";
    exit(); 
}

if ($oldpass == $_SESSION['password']) {
    echo "session and oldpass are matching";
} else {
    echo "Session and oldpass do not match!";
}

$isOwner = "no";
//check if user is logged in owner of account
if($u == $log_username && $user_ok == true){
    $isOwner = "yes";
}
$newpass = password_hash($newpass, PASSWORD_BCRYPT);

if (isset($_POST["submit"]) && ($isOwner == "yes") && ($user_ok == true) && ($newpass == $repeatpass)) {
    $newpass = password_hash($newpass, PASSWORD_BCRYPT);
    $sql = "UPDATE users SET `password`='$newpass' WHERE username='$u' LIMIT 1";
}

if (mysqli_query($db_conx, $sql)) {
    echo "Record updated successfully";

} else {
    echo "Error updating record: " . mysqli_error($db_conx);
    }

?>

<h3>Create new password</h3>
  <form action="" method="post">
    <div>Current Password</div>
    <input type="text" class="form-control" id="password" name="oldpass" ><?php echo "{$oldpass}"; ?>
    <div>New Password</div>
    <input type="text" class="form-control" id="password" name="newpass" ><?php echo "{$newpass}"; ?>
    <div>Repeat Password</div>
    <input type="text" class="form-control" id="password" name="repeatpass" ><?php echo "{$repeatpass}"; ?>
    <br /><br />
    <input type="submit" name="submit" value="Submit"> 
    <p id="status" ></p>
  </form><?php echo "{$oldpass}, {$_SESSION['password']}"; ?>


  <pre>
  <?php
  var_dump($_SESSION);
    var_dump($oldpass);

    var_dump($newpass);
    var_dump($repeatpass);
    ?>
  </pre>
  • Whats your php version? – Mihai Feb 20 '15 at 11:32
  • My PHP version is 5.6.3. – EntangledQuantum Feb 20 '15 at 11:33
  • You complicate yourself, any reason you dont use default hashing function in php >5.5? https://php.net/manual/ro/function.password-hash.php – Mihai Feb 20 '15 at 11:36
  • I didn't know about that function so i tried to use crypt(), but i tried to implement your function but when i vardump $newpass its in plain text. should i use the password_hash function outside of the if Isset statement? – EntangledQuantum Feb 20 '15 at 11:43
  • Related : [How can I store my users' passwords safely?](http://stackoverflow.com/questions/1581610/how-can-i-store-my-users-passwords-safely) and [How do you use bcrypt for hashing passwords in PHP?](http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php) – slaur4 Feb 20 '15 at 11:53
  • Thanks i already use the password_hash function :) i updated the code in the OP. now i need to figure out how i can get the password thats in the db. – EntangledQuantum Feb 20 '15 at 12:02

1 Answers1

0

Ther is a much easier way to solve the problem:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);

The algorithm MD5 is not a good choice to protect passwords because it is designed to be fast and can be brute-forced too easily.

Storing the password/hash in the session is not very helpful, if you know it is the same user, you know if he is already logged in, just store an indicator in the session like $_SESSION['is_logged_in'] or just the username $_SESSION['username'].

martinstoeckli
  • 23,430
  • 6
  • 56
  • 87
  • Great the crypt function works now. But i still struggle to figure out why my code doesnt retrieve the password which is already stored in the db i don't know with which name i should call it. thanks for your help! – EntangledQuantum Feb 20 '15 at 12:01
  • @EntangledQuantum - You didn't do anything with the result of your query, the password should be part of the result like `$user_query->password`. – martinstoeckli Feb 20 '15 at 12:14
  • ok i am experimenting with the query but it doesn't work, I am doing something wrong. What exactly would i have to write? – EntangledQuantum Feb 20 '15 at 12:25
  • @EntangledQuantum - In a first step i would concentrate only on storing and retrieving rows from the database. When you do this, it is propably a good time to switch to PDO, which makes handling easier and safer (your queries are prone to SQL-injection at the moment). Have a look for a good tutorial, maybe what google finds: [PDO Tutorial for MySQL Developers](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – martinstoeckli Feb 20 '15 at 12:35