1

我正在创建一个登录系统。但无论出于何种原因,我都无法获得正确验证的密码。当我知道密码正确时,它一直告诉我密码错误。这是表单代码:

<form id="login" action="login_processing.php" method="post">
    <label2>Email</label2>
    <input type="text" name="email">
    <label2>Password</label2>
    <input type="password" name="password">
    <a class="forgot_password" href="forgot.php">Forgot password?</a>
    <input type="submit" class="custom_submit" name="sign_in" value="Sign In">
    <br />
    <br />
    <br />
    <a href="#sliderName" class="next">Create Account</a>
</form>

这里是login_processing.php上处理登录信息的代码:

require_once 'database_connect.php';
session_start();

$email = $_POST['email'];
$password = $_POST['password'];

$email = mysql_real_escape_string($email);
$findLogin = "SELECT password, salt FROM client_login WHERE email = '$email';";
$loginResult = mysql_query($findLogin);
if (mysql_num_rows($loginResult) < 1){
    $_SESSION['errNo_account'] = 'No account with that email exists';
    header('Location: login.php');
}
$userData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password));
if ($hash != $userData['password']){
    $_SESSION['errPassword_incorrect'] = 'Incorrect Password';
    header('Location: myaccount.php');
}
else{
    $idSelect = "SELECT client_id FROM client_login WHERE email = '$email' ";
    $idResult = mysql_query($idSelect);
    while($idData = mysql_fetch_assoc($idResult)){
        $_SESSION['client_id'] = $idData['client_id'];
    }

}

这是对密码进行哈希处理并添加盐的原始注册页面:

function createSalt(){
        $string = md5(uniqid(rand(), true));
        return substr($string, 0, 3);
    }
        $salt = createSalt();
        $hash = hash('sha256', $salt . $hash);  

    $insertLogin = "INSERT INTO client_login(email, password, salt, created) VALUES('$email','$hash', '$salt', '$today')";
    mysql_query($insertLogin);

它一直告诉我我的密码不正确......当我知道它不正确时。我似乎无法弄清楚错误是什么,它让我发疯!!!

任何帮助将不胜感激。

4

2 回答 2

1

在您的注册脚本中,您使用的是连接盐和 pw 的单个哈希,但在您的登录脚本中,您使用的是嵌套的 hash() 调用。

登记:

$hash = hash('sha256', $salt . $hash);

登录:

$hash = hash('sha256', $userData['salt'] . hash('sha256', $password));

注意区别?

于 2013-01-30T02:14:14.943 回答
0

这是我搞砸的地方..在我匆忙中,我从未真正在创建新帐户时对密码进行哈希处理。所以它没有散列任何东西....只是它本身。因此,我在“新帐户”页面中添加了一行(以散列原始密码)并解决了整个问题:

function createSalt(){
        $string = md5(uniqid(rand(), true));
        return substr($string, 0, 3);
    }
        $hash = hash('sha256', $password); //this was forgotten originally :(
        $salt = createSalt();
        $hash = hash('sha256', $salt . $hash);  

现在添加了上面的代码可以正常工作。谢谢!

于 2013-01-30T02:33:57.497 回答