【发布时间】:2015-12-01 18:57:08
【问题描述】:
我正在学习 PHP,作为一个项目,我开始构建一个社交网络。我确实创建了注册表单和登录表单,并且可以将用户添加到我的数据库中。我也散列他们的密码。这是一个简单的网站,并且正在进行中,因此存在很多安全漏洞。
我的问题在于登录文件,我似乎无法将用户与他给我的密码相匹配。为了验证用户密码,我使用了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"; 语句。
【问题讨论】:
-
你在
sanitize()函数中对$myPassword做了什么?向我们展示该代码 -
您对 SQL 注入敞开了大门。如果有人说他们的用户名是
'; DROP TABLE members;,你的 SQL 语句现在计算为SELECT * FROM members WHERE Username=''; DROP TABLE members;'。你需要使用PDO 之类的东西。 (我知道你说有安全漏洞,但这是一个很大的,所以我忍不住提一下) -
@RiggsFolly 我添加了清理功能的代码
-
@bytesized 正如我提到的,这是正在进行中的工作,我首先要确保基础工作正常,然后我将处理 SQL 注入等
-
“当我第一次创建数据库时,我使用 CHAR(10) 作为密码,而哈希密码需要更多字符。” - 你猜怎么着,你有 50 个字符短.我就知道。这就是为什么我删除了我的评论,询问你它有多长和类型。可能没看过,现在只有主知道了。
标签: php sql validation password-encryption