使用加密检查数据库

时间:2014-11-23 16:25:42

标签: php mysql forms encryption

我想在页面'encryptionmachine1.php'中对数据库运行查询,以确保输入的密码正确无误。为了保证安全,我首先要求页面加密输入的密码,然后检查数据库字段'EncryptedPasswords'以查看它是否存在。在我输入正确的密码(number1)时,仅显示“pwd不存在”消息。我也使用md5()函数来加密密码。有帮助吗?谢谢Dan

<?php

if(isset($_POST['submit'])){
$str=$_POST['pwd'];
md5($str);
$dblink=mysql_connect("localhost","Dan");
mysql_select_db("Dan");
$rs=mysql_query("SELECT * FROM passwords WHERE EncryptedPassword='".$str."'");
if($row = mysql_fetch_assoc($rs)){
$dbPassword=$row['EncryptedPassword'];
echo "password exists";
header('Location:http://localhost/encryptionmachine2.php?pwd='.$str);//http://194.36.155.250/POO12104368/encryptionmachine2.php
}else{
echo"pwd does not exist";

}

 }

?>
<html>
<head>

<title>EncryptionMachine1</title>

</head>
<body>

<form name="myForm" action="#" method="POST"> 
<p>Pwd:<input type="text" name="pwd"></p>
<input type="submit" value="Submit" name="submit">
</form>

</body>
</html> 

1 个答案:

答案 0 :(得分:2)

为了确保代码安全,有很多事情需要更改。最紧迫的两个是:

  1. You want to hash passwords, not encrypt them
  2. 您想use prepared statements
    • 这必然意味着停止使用mysql_query()mysql_fetch_assoc()来支持PDO或mysqli。

我强烈建议从A Gentle Introduction to Application Security开始。看起来很多,但我保证这是可以管理的,并且您可以做到。