Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单中提交的密码与数据库中的密码不匹配_Php_Login_Pdo_Md5 - Fatal编程技术网

Php 表单中提交的密码与数据库中的密码不匹配

Php 表单中提交的密码与数据库中的密码不匹配,php,login,pdo,md5,Php,Login,Pdo,Md5,我编写了一个登录表单,点击提交按钮后,我想检查数据库中是否存在该用户 if(isset($_POST['submitBtn'])){ if($_POST['senderLogin'] == 'customer'){ $checkIfExists = new CustomersDAO(); $stam = $checkIfExists->isExist($_POST["name"], $_POST["password"]); var_d

我编写了一个登录表单,点击提交按钮后,我想检查数据库中是否存在该用户

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表中的用户,但没有成功


我尝试只匹配名称,结果成功了,因此问题在于提交的密码与数据库中的密码的比较。

您需要使用md5加密密码

if(isset($_POST['submitBtn'])){
    if($_POST['senderLogin'] == 'customer'){
        $checkIfExists = new CustomersDAO();
        $stam = $checkIfExists->isExist($_POST["name"], $_POST["password"]);
        var_dump($stam);
    }
}
if(isset($_POST['submitBtn'])){
    if($_POST['senderLogin'] == 'customer'){
        $checkIfExists = new CustomersDAO();
        $stam = $checkIfExists->isExist($_POST["name"], md5($_POST["password"]) );
        var_dump($stam);
    }
}

您的代码似乎没有问题,而且

我试着只匹配名称,结果成功了,所以问题在于将提交的密码与数据库中的密码进行比较

if(isset($_POST['submitBtn'])){
    if($_POST['senderLogin'] == 'customer'){
        $checkIfExists = new CustomersDAO();
        $stam = $checkIfExists->isExist($_POST["name"], $_POST["password"]);
        var_dump($stam);
    }
}

当对密码或敏感数据使用散列函数时,开发人员通常会在其中附加盐字符串,以帮助防止这些散列容易被破坏。我看到在你的代码中,你只是在密码上使用md5,没有任何盐。也许在脚本的另一点上,例如,在将用户添加到数据库的函数中,您正在用一些盐对密码进行散列,显然散列不匹配。

OT但MD5不是加密,不推荐使用散列密码的方法。您是否检查了$\u POST[password]中的密码是否正确?MD5要让您开始调试并帮助您解决任何未来的问题,请尝试回显您的查询。确保它所查找的是您认为的内容,并查看它是否与数据库中的内容匹配。通常情况下,问题是人们在数据库中存储密码时忘记了散列密码,更好的是,尽管扔掉它,使用一个预先制作的身份验证库,因为对于粗心的人来说有很多陷阱,除非你有高度定制的要求,否则滚动你自己的身份验证库是没有意义的。你确定列的长度对于你的md5加密通行证来说足够长吗?我曾经有25的长度,有时太短了,我会遇到和你现在面临的问题一样的问题。一个人不只是用md5加密任何东西,而且他已经在这样做了,当他绑定他的参数时,我在isExist函数中做了,这似乎是一个评论,而不是一个答案。