gpt4 book ai didi

php - MySQL SHA() 不起作用

转载 作者:行者123 更新时间:2023-11-29 03:04:46 24 4
gpt4 key购买 nike

所以基本上我在我的注册表中加密了密码,就像这样:

    $query = "INSERT INTO users(first_name, last_name, email, password, 
username) VALUES ('$fn', '$ln', '$em', SHA('$pw1'), '$un')";

现在密码已经过哈希处理,但是当我尝试在我的登录脚本中使用它时,它不起作用并且函数 mysql_num_rows 返回 0。

    <?php
ob_start();
//If login button is pressed
if(isset($_POST['submitted'])){
//Username clean up
if(preg_match('%^[a-zA-Z0-9_-]{6,20}$%', stripslashes(trim($_POST['username'])))){
$u = escape_data($_POST['username']);
} else {
$u = FALSE;
echo '<p><font color="red" size="+1">Please enter valid username</font></p>';
}

//Password clean up
if(preg_match('%^[a-zA-Z0-9_-]{6,20}$%', stripslashes(trim($_POST['password'])))){
$p = escape_data($_POST['password']);
} else {
$p = FALSE;
echo '<p><font color="red" size="+1">Please enter valid password</font></p>';
}

//Check if both matched
if($u && $p){
$query = "SELECT * FROM users WHERE username='$u' AND password=SHA('$p')";
$result = mysql_query($query);
$count = mysql_num_rows($result);
$row = mysql_fetch_array($result, MYSQL_NUM);
if($count != 0){
$_SESSION['username'] = $row[1];
$_SESSION['password'] = $row[3];
header("Location: login_confirmed.php");
} else {
echo "Wrong username or password!";
}
}
}
ob_end_flush();
?>

最佳答案

这可能是因为您的密码字段类型。您的密码字段是 VARCHAR 吗?多久了?在我看来,SHA 生成的字符串比该字段允许的长度更长,因此它在存储时会被截断,并且在您重新生成它以进行检查时它确实匹配。

MySQL documentation说你需要 40 个字符来存储 SHA 的输出。

Calculates an SHA-1 160-bit checksum for the string, as described in RFC 3174 (Secure Hash Algorithm). The value is returned as a string of 40 hex digits, or NULL if the argument was NULL. One of the possible uses for this function is as a hash key.

关于php - MySQL SHA() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17547738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com