在PHP中对密码进行哈希运算,然后将其发送到数据库,从而得到错误信息
我已经研究了一段时间,甚至得到了我的主机公司的帮助,但我遇到了一个问题,我的PHP代码和我的数据库通过我的网站。虽然我的代码对我输入的密码进行了哈希运算,但当我尝试使用常规单词password时,它显示为不正确。但是如果我复制并粘贴散列密码,它就可以工作了在PHP中对密码进行哈希运算,然后将其发送到数据库,从而得到错误信息,php,mysql,hash,Php,Mysql,Hash,我已经研究了一段时间,甚至得到了我的主机公司的帮助,但我遇到了一个问题,我的PHP代码和我的数据库通过我的网站。虽然我的代码对我输入的密码进行了哈希运算,但当我尝试使用常规单词password时,它显示为不正确。但是如果我复制并粘贴散列密码,它就可以工作了 <?php /* NEW.PHP Allows user to create a new entry in the database */ // creates the new record form // since th
<?php
/* NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($email, $pass, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New User</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '') {
echo '<div style="padding:4px; border:1px soluser_id red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<strong>Update User Info <br><br><br><br><br>Email: *</strong>
<input type="text" name="email" value="<?php echo $email; ?>" /><br/>
<strong>Password: *</strong> <input type="password" name="pass" value="<?php echo $pass; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit"> <br><br>Back to <a href="index2.html">home</a>?</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit'])) {
// get form data, making sure it is valuser_id
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$pass = mysql_real_escape_string(htmlspecialchars($_POST['pass']));
// check to make sure both fields are entered
if ($email == '' || $pass == '') {
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($email, $pass, $error);
} else {
// save the data to the database
mysql_query("INSERT users SET email='$email', pass=MD5('$pass')")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
} else {
// if the form hasn't been submitted, display the form
renderForm('','','');
}
?>
新用户
更新用户信息
电子邮件:
我会在PHP端执行MD5哈希。在它进入数据库之前打印它,并尝试将其与登录表单上给出的输入进行比较
在这种情况下也不需要htmlspecialchars
。既然你逃得很好。如果它包含奇怪的字符,它会将它们与数据库进行匹配
另外,请确保在两个页面上都设置了编码类型,并确保它们相同。如果在登录表单中没有看到SELECT查询,我会问您在选择时是否使用MD5哈希
mysql_query("SELECT * FROM users WHERE email='$email' AND pass=MD5('$pass')")
or die(mysql_error());
但是,我同意您不应该使用MD5进行密码散列。查看如果您的目的是通过在存储密码之前对密码进行散列来增加安全性,md5
是一个糟糕的选择。请阅读BCrypt。您应该使用准备好的语句与数据库进行交互,您正在向SQL注入敞开大门。请确保将md5密码检查为保存在数据库中的密码,而不是密码本身。请不要使用您自己的密码哈希。PHP提供并请使用它们。如果您使用的是5.5之前的PHP版本,则需要在稍后演示如何进行验证。上面的代码没有“明显”的错误,除了使用不推荐使用的mysql_*()函数和md5。我想我现在明白了,因此,当我让用户登录时,我想确保他们键入的任何内容都会被散列,然后与我的数据库条目进行比较?@StevenEck你真的不应该使用这种方法。阅读问题下方的所有评论,警告你这一点,并推荐实际的散列,如password\u hash()
(以及对mysql的使用)。你写了“你想让它先工作”,但如果你想把房子漆成蓝色,为什么你要先把它漆成绿色呢?这是工作的两倍,没有什么用处。我想我会研究另一种方法,但我现在明白了要点。我必须比较苹果和苹果。我在道义上不得不否决这个问题:它通过同时做两件非常错误的事情来促进可怕的安全性:鼓励使用官方不推荐使用mysql\u
函数,并使用MD5进行密码散列。改用PDO+Bcrypt!@StevenEck嗯,有点-当然,你不能将苹果和香蕉进行比较。但是如果你使用password\u散列()
,你需要使用password\u verify()
来验证它(手册中有很好的例子: