我正在学习PHP,并将其作为一个项目开始构建社交网络。我确实创建了注册表单和登录表单,并且可以将用户添加到我的数据库中。我也散列他们的密码。这是一个简单的网站和正在进行的工作,因此存在很多安全漏洞。如何使用password_verify()从数据库中检索密码?
我的问题是与登录文件,我似乎无法匹配用户与他给我的密码。为了验证用户密码,我使用了password_verify()
函数,但它似乎并不正确。
这里是我的代码:
注册
<?php
//signUp.php
//Here is where I add a user in my database
//I validate the input, confirm that the password is written like it should be
//check if a user with the same username exists in the database
//if all checks out I will add the user in the database
//and redirect the user to his profile
require_once 'login.php';
require_once 'helperFunctions.php';
$conn = new mysqli($servername, $username, $password, $database);
if(!$conn)
die("Connection failed:" . mysqli_connect_error());
$myUsername = $_POST['Name'];
$myPassword = $_POST['Password'];
$myConfirm = $_POST['conPass'];
sanitize($conn, $myUsername);
sanitize($conn, $myPassword);
//check if the two passwords are the same
if($myPassword != $myConfirm){
print "Your passwords don't match";
header("refresh: 5; index.html");
} else {
//check if username already exists in database
$query = "SELECT * FROM members WHERE Username='$myUsername'";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
if($count == 0){
//hash password
$hashedPass = password_hash("$myPassword", PASSWORD_DEFAULT);
//username doesn't exist in database
//add user with the hashed password
$query ="INSERT INTO members (Username, Password) VALUES ('{$myUsername}', '{$hashedPass}')";
$result = mysqli_query($conn, $query);
if(!$result)
die("Invalid query: " . mysqli_error());
else{
print "You are now a member or The Social Network";
header("refresh: 5; login_success.php");
}
} else {
print "Username already exists";
header("refresh: 5; index.html");
}
}
?>
登录
<?php
//checkLogin.php
//Here is where I authenticate my users and if successfull I will show them their profile
require_once 'login.php';
require_once 'helperFunctions.php';
$conn = new mysqli($servername, $username, $password, $database);
if(!$conn)
die("Connection failed:" . mysqli_connect_error());
//Values from form
$myUsername = $_POST['Name'];
$myPassword = $_POST['Password'];
//sanitize input
sanitize($conn, $myUsername);
sanitize($conn, $myPassword);
$query = "SELECT * FROM members WHERE Username='$myUsername'";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
if($count == 1){
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
print "hashedPass = ${row['Password']}";
print "myPassword: " . $myPassword;
if(password_verify($myPassword, $row['Password'])){
print "Password match";
} else
print "The username or password do not match";
}
?>
消毒功能
function sanitize($conn, $val){
$val = stripslashes($val);
$val = mysqli_real_escape_string($conn, $val);
}
通过运行程序print "hashedPass = ${row['Password']}";
打印出散列的密码,这与我在我的数据库中使用的密码相同,但由于某种原因,我在此之后重定向到print "The username or password do not match";
声明。
你在做什么来'$ myPassword'在'的sanitize()'函数?告诉我们该代码 – RiggsFolly
你正在为SQL注入开放。如果有人说他们的用户名是“',该怎么办? DROP TABLE members;'你的SQL语句现在评估为'SELECT * FROM members WHERE Username =''; DROP TABLE成员;''。你需要使用像[PDO](http://php.net/manual/en/book.pdo.php)。 (我知道你说有安全漏洞,但这是一个很大的,所以我忍不住提起它) – bytesized
@RiggsFolly我添加了sanitize函数的代码 – captain