【发布时间】:2015-05-18 02:28:29
【问题描述】:
我使用哈希和盐创建了我自己的 login.php 方式。当我输入新的管理员帐户时,密码似乎在我的 phpmyadmin 上工作正常,但是当我在登录中调用哈希和盐时,“用户不存在”似乎很好。即使我输入了正确的凭据,错误始终是“用户名和密码不正确”。我已经在我以前的登录系统中使用过这个脚本,但我不知道为什么它现在可以工作了。
这是我的 login.php
<?php
include_once('config.php');
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($mysqli,$username);
$query = $mysqli->query("SELECT password,salt FROM admin WHERE username='$username'");
$numrows = mysqli_num_rows($query);
if($username == "" && $password =="") {
echo '<center>';
echo '<h3>Please Fill in the blank form</h3>';
echo '<button type="button" class="btn btn-primary" id="goback" >Log In Again</button>';
echo '</center>';
} else if($numrows!=0) {
$userData = mysqli_fetch_array($query, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password));
if($hash != $userData['password']) {
echo '<center>';
echo '<h3>Incorrent username or password</h3>';
echo '<button type="button" class="btn btn-primary" id="goback" >Log In Again</button>';
echo '</center>';
} else {
header('Location: firstpagesurvey.html');
}
} else {
die('
<center>
<h3>Username does\'t exists</h3>
<button type="button" class="btn btn-primary" id="goback" >Log In Again</button>
</center>
');
}
?>
这里是 create_admin 操作
<?php
include_once('config.php');
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$hash = hash('sha256', $password);
function createSalt(){
$text = md5(uniqid(rand(), true));
return substr($text,0,3);
}
$salt = createSalt();
$password = hash('sha256', $hash.$salt);
$username = $mysqli->real_escape_string($username);
$query = $mysqli->query("INSERT INTO `admin`(`username`,`password`,`firstname`,`lastname`,`salt`) VALUES('$username','$password','$firstname','$lastname','$salt')");
$mysqli->close();
header('Location: success.php');
?>
【问题讨论】:
-
保存用户名和密码的代码在哪里?
-
您为什么不检查对
mysqli的调用的响应是否有错误? -
Norberh 你是什么意思?我有 login.html。
-
流浪汉如何查看回复?
-
您在注册用户时是否使用哈希和盐,以便使用哈希和盐将密码保存在数据库中?
标签: php mysql hash mysqli salt