Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.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_Mysql - Fatal编程技术网

Php 检查用户名是否存在,并检查密码是否匹配

Php 检查用户名是否存在,并检查密码是否匹配,php,mysql,Php,Mysql,你好,我正在学习如何使用事先准备好的语句。我已经了解了如何检查密码和电子邮件地址是否匹配,但是我希望在参数检查中有一个标准,同时检查电子邮件地址是否在系统中,以及检查密码是否不匹配 我如何添加“IF/ELSE”参数来检查电子邮件地址,然后检查密码是否匹配(目前它是这样做的) 如有任何帮助,将不胜感激: $emailAddress = $_POST['emailAddress']; $password = $_POST['password']; if ($stmt = $conn->pre

你好,我正在学习如何使用事先准备好的语句。我已经了解了如何检查密码和电子邮件地址是否匹配,但是我希望在参数检查中有一个标准,同时检查电子邮件地址是否在系统中,以及检查密码是否不匹配

我如何添加“IF/ELSE”参数来检查电子邮件地址,然后检查密码是否匹配(目前它是这样做的)

如有任何帮助,将不胜感激:

$emailAddress = $_POST['emailAddress'];
$password = $_POST['password'];

if ($stmt = $conn->prepare("SELECT `password` FROM `users` WHERE emailAddress=?")) {

    $stmt->bind_param("s", $emailAddress);
    $stmt->execute();

    $stmt->bind_result($result);
    $stmt->fetch();
    $stmt->close();
}


if(password_verify($password, $result)){
    // Login if the email and password matches
    session_start();
    $_SESSION['loggedin'] = true;
    $_SESSION['emailAddress'] = $emailAddress;
    header('Location: ../index.php');
}
else{

    header('Location: ../login.php?error=1');
}

$conn->close();
将您的代码修改为:

$emailAddress = $_POST['emailAddress'];
$password = $_POST['password'];

if ($stmt = $conn->prepare("SELECT `password` FROM `users` WHERE emailAddress=?")) {

    $stmt->bind_param("s", $emailAddress);
    $stmt->execute();

    $stmt->bind_result($result);
    $stmt->fetch();

    /** 
     * Another option can be 
     * if ($stmt->fetch()) { 
     */

    if (!empty($result)) {
        // something is found
        if (password_verify($password, $result)){
            // Login if the email and password matches
            session_start();
            $_SESSION['loggedin'] = true;
            $_SESSION['emailAddress'] = $emailAddress;
            header('Location: ../index.php');    // or whatever
            exit;
        } else {
            // No password match
            header('Location: ../login.php?error=1');    // or whatever
            exit;
        }
    } else {
        // No email found
        header('Location: ../login.php?error=2');    // or whatever
        exit;
    }
    $stmt->close();
}

你的问题是什么?@u_mulder“我希望在参数检查中有一个标准,也就是电子邮件地址是否在系统中,以及检查密码是否不匹配。”这不是问题。这是一个愿望。什么是参数?我重新措辞以满足you@danjbh任何方式都可以;例如,我将使用一个查询,但这只是我自己,它避免了更多的代码和函数调用。应该将exit添加到每个头;-)@弗雷德二世-肯定,固定)我选择使用这个,因为我可以理解它