简体   繁体   English

具有用户名和电子邮件的PDO登录系统

[英]PDO Login System With Username and Email

I'm trying to figure out how to use PDO to login to my site give the user the option of either their email address or username once they are logged in, I checked some of the other answers but it doesn't seem to work for me. 我试图弄清楚如何使用PDO登录到我的网站,以便在用户登录后为用户提供其电子邮件地址或用户名的选项,我检查了其他一些答案,但似乎不起作用我。

Here is the code 这是代码

<?php

if(isset($_POST['username']) || isset($_POST['password'])){
    if(!$_POST['username'] || !$_POST['password']){
        $error = "Please Enter your Username and Password";
    }

So the issue stems from below, I tried adding an OR on the $query as I saw it from one of the other posts on here but doing that allows the user to login through email but not with username, if I remove "OR user_email" they can login through username but not E-Mail. 所以问题出在下面,我尝试在$ query上添加一个OR,就像我从此处其他帖子之一看到的那样,但是这样做可以使用户通过电子邮件登录,但不能使用用户名登录,如果我删除“ OR user_email”他们可以通过用户名登录,但不能通过电子邮件登录。

if(!$error){
            //No errors - lets get the users account
            $query = "SELECT * FROM users WHERE user_name OR user_email = :username";

            $result = $DBH->prepare($query);
            $result->bindParam(':username', $_POST['username']);
            $result->execute();

            $row = $result->fetch(PDO::FETCH_ASSOC);

            if($row){
                //User found - let’s check the password
                if(password_verify($_POST['password'], $row['user_password'])){
                    $_SESSION['loggedin'] = true;
                    $_SESSION['userData'] = $row;

                echo "<script> window.location.assign('index.php?p=viewprofile'); </script>";
            }else{
                $error = "Username/Password Incorrect";
            }

        }else{
            $error = "Username/Password Incorrect";
        }

    }
}

?>

You SQL query is wrong: 您的SQL查询错误:

$query = "SELECT * FROM users WHERE user_name = :username OR user_email = :username";

You forgot to compare the column user_name with the user input 您忘记将user_name列与用户输入进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM