我是MySQL和PHP的新手,所以道歉,但我在尝试让我的代码工作时遇到了问题。用户可以使用用户名和密码在我的网站上注册。我知道应该使用名为salt的加密方法存储密码。所以现在我已经完成了这项工作,我希望用户能够登录,而且我不确定我是否正确行事。
目前我收到错误:
Fatal error: Call to undefined function db() in C:\xampp\htdocs\hewden\ssa\suppliers\include\validate_login.php on line 16
这是我的注册码:
<?php
session_start();
include("../include/config.php");
//retrieve our data from POST
$contactname = $_POST['contactname'];
$compname = $_POST['compname'];
$username = $_POST['emailaddress'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
if($password1 != $password2) {
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Password's did not match.</p> </div>';
header("location:..\sign-up.php");
}else{
if(strlen($username) > 30) {
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Ooops!</h23><p>The Username you have selected is incorrect.</p> </div>';
header("location:..\sign-up.php");
}else{
$hash = hash('sha256', $password1);
function createSalt(){
$text = md5(uniqid(rand(), true));
return substr($text, 0, 3); }
$salt = createSalt();
$password = hash('sha256', $salt . $hash);
$username = mysql_real_escape_string($username);
$query = "INSERT INTO supplier_pre_sign (contact_name, company_name, supplier_email, password, salt, date) VALUES ('$contactname','$compname','$username', '$password', '$salt', now())";
mysql_query($query); //You forgot to add this line
echo $salt;
} }?>
并将其存储在我的表格&#39; supplier_pre_sign&#39;像这样:
ID (AI) | contact_name | company_name | supplier_email | password | salt
1 Dave Hewden hewden@hewden.com test(hashed) 431
一旦注册获得批准,他们就会被复制到我的其他表格&#39; supplier_users&#39;
这张表是这样的:
ID | USERNAME | PASSWORD | SALT | ONLINE
1 hewden@hewden.com test(hashed) 431 Offline
这是我的登录代码:
<?php
session_start();
include("config.php");
$tbl_name="internal_users";
$tbl_name2="supplier_users";
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "select * from $tbl_name where username = '$myusername' and password = '$mypassword',
union
select * from $tbl_name2 where username = '$myusername' and password = '$mypassword'";
$sql = db("select * from $tbl_name where username = '$myusername' and password = '$mypassword',
union
select * from $tbl_name2 where username = '$myusername' and password = '$mypassword'",
$_POST['myusername'], hmac_hash("sha256", $_POST['mypassword'], $salt));
if(numRows($res) > 0) {
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);
if($count==1){
session_start();
$_SESSION['user']=$myusername;
$_SESSION['username']=$row['First_Name'];
if(isset($_SESSION['val']))
$_SESSION['val']=$_SESSION['val']+1;
if($result){
$sql2 = "UPDATE $tbl_name2 SET online = 'online' WHERE online = 'offline'";
$result2=mysql_query($sql2); }
else
$_SESSION['val']=1;
header("location:../dashboard.php");
}
else {
echo mysql_error();
$_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);"></div><h23>Oooops!</h23><p>The Username and Password Combination do not match. Please try again.</p> </div>';
header("location:../index.php");
} }
ob_end_flush();
?>
现在我的问题是如何让我的登录脚本对用户进行身份验证,因为我不太了解盐的工作方式,我的用户只需键入&#39; test&#39;期望它登录,所以如何通过检查salt密码匹配来登录?
提前致谢
答案 0 :(得分:1)
为此,PHP现在有一个内置函数password_hash()。该函数生成一个长度为60个字符的BCrypt哈希值(包括盐),因此您不必单独存储盐。
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
P.S。不要使用任何哈希算法,如SHA- *或MD5,因为它们没有成本因素,而且速度太快。有关详细信息,您可以查看我的tutorial关于安全存储密码的信息。
答案 1 :(得分:0)
你是正确的,因为使用盐是一个好主意,但你似乎误解了如何使用它。 Salt不是一个单独的密码,而是在对其进行哈希处理时添加到密码中的额外数据,这样如果您的数据库受到损害,它就会更难破解。
阅读像Secure Salted Password Hashing - How to do it Properly这样的好教程可以帮助您更好地理解这个想法。
因此,当您对密码进行哈希处理时,您应该执行以下操作:
$hash = hash('sha256', $password1.$salt);
NB - 不是PHP程序员,所以语法错误是我
对于登录用户,您需要首先根据用户名从数据库中查找salt,然后使用salt对提供的密码进行哈希处理,然后再针对数据库检查哈希值。
请注意,使用预构建的库(例如PHPass)进行密码散列而不是自己实现
几乎总是好得多。