简体   繁体   English

PHP md5哈希无法正常工作

[英]PHP md5 hashing not working

I've got the following PHP code which is used for registering to a website. 我有以下用于注册到网站的PHP代码。 I'm trying to hash the passwords for security but whenever I submit a dummy registration the passwords are not hashed in phpMyAdmin. 为了安全起见,我试图对密码进行哈希处理,但是每当我提交虚拟注册时,密码都不会在phpMyAdmin中进行哈希处理。 They appear normal. 他们看起来很正常。 Here is my code: 这是我的代码:

<?php

//get the values from the form
$Name = $_POST['name'];
$Username = $_POST['username'];
$Password = $_POST['password'];
$RepeatPassword = $_POST['repeatpassword'];

//encrypt the passwords
md5($Password);
md5($RepeatPassword);

//query the database
$query = "INSERT INTO users VALUES ('', '$Name', '$Username', '$Password')";

if (!mysql_query($query)) {

die('Error ' . mysql_error() . ' in query ' . $query);
} 

//check passwords match
if ($Password !== $RepeatPassword) {
echo "Your passwords do not match. <a href='login.php'>Return to login page</a>";

}

//check to see if fields are blank
if ($Name=="") {
echo "Name is a required field. <a href='login.php'>Return to login page</a>";
}

else if ($Username=="") {
echo "Username is a required field. <a href='login.php'>Return to login page</a>"; 
}

else if ($Password=="") {
echo "Password is a required field. <a href='login.php'>Return to login page</a>";
}

else if ($RepeatPassword=="") {
    echo "Repeat Password is a required field. <a href='login.php'>Return to login page</a>";
}

else {
    $_SESSION["message"] = "You have successfully registered! Please login using your username and password.";
    header("Location: login.php");
}
?>

The tutorials I have read online have all said to do it as per the above. 我在网上阅读的所有教程都说可以按照上述方法进行。 I've tried putting the two lines of md5 code in numerous places but to no avail. 我曾尝试将md5代码的两行放在许多地方,但无济于事。

md5($Password);
md5($RepeatPassword);

This code basically does nothing. 这段代码基本上什么也不做。 You want: 你要:

$Password = md5($Password);
$RepeatPassword = md5($RepeatPassword);

But ultimately, MD5 doesn't do much for security. 但是最终,MD5在安全性方面做得并不多。 Consider bcrypt , stop using the mysql_* functions, and start learning about SQL injection attacks. 考虑使用bcrypt ,停止使用mysql_*函数,并开始学习有关SQL注入攻击的知识。

You're not doing anything with the return value of the functions. 您不会对函数的返回值做任何事情。 It should be: 它应该是:

$Password = md5($Password);

$RepeatPassword = md5($RepeatPassword);

It's not working because you're not assigning your md5 to a variable. 它不起作用,因为您没有将md5分配给变量。 Do something like this: 做这样的事情:

$Password = md5($Password);

$RepeatPassword= md5($RepeatPassword);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM