我写了一个登录表单,点击提交按钮后,我想检查数据库中是否存在用户。
if(isset($_POST['submitBtn'])){
if($_POST['senderLogin'] == 'customer'){
$checkIfExists = new CustomersDAO();
$stam = $checkIfExists->isExist($_POST["name"], $_POST["password"]);
var_dump($stam);
}
}
我喜欢的检查是:
public function isExist($name, $password) {
$this->connect();
if($this->con){
$sql = "SELECT * FROM customers WHERE name=? AND password=?";
$stmt = $this->db->prepare($sql);
$password = md5($password);
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $password);
$stmt->execute();
$fetched = $stmt->fetchColumn();
$this->disconnect();
if($fetched > 0) {
return true;
} else {
return FALSE;}
}
}
数据库中的密码使用md5加密。
我试图键入一个存在于customers表中的用户,但没有成功。
我尝试只匹配名称,结果成功了,所以问题是将提交的密码与数据库中的密码进行比较。
您的代码似乎没有什么问题,而且。。。
"我试着只匹配名字,结果成功了,所以问题是将提交的密码与数据库中的密码进行比较。"
当将散列函数与密码或敏感数据一起使用时,开发人员通常会在其上附加"salt"字符串,以帮助防止这些散列容易被破坏。我看到在你的代码中,你只在密码上使用md5()
,没有任何盐。也许在脚本的另一点上,例如,在将用户添加到数据库的函数中,您正在用一些盐对密码进行哈希,显然哈希不匹配。
您需要使用md5 加密密码
if(isset($_POST['submitBtn'])){
if($_POST['senderLogin'] == 'customer'){
$checkIfExists = new CustomersDAO();
$stam = $checkIfExists->isExist($_POST["name"], md5($_POST["password"]) );
var_dump($stam);
}
}