How to allow user to login with both username and email in php?

Rahul Kapoor

i am currently having problem allowing user to login with both username or email and entered and matching it with the database, here is my code:

<?php
//Login Script
if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {
    $user_login = $_POST["user_login"]; 
    $password_login = $_POST["password_login"];
    $md5password_login = md5($password_login);
    $sql = mysqli_query($conn, "SELECT id FROM users2 WHERE username='$user_login' AND password='$md5password_login' LIMIT 1"); // query the person

    //Check for their existance
    $userCount = mysqli_num_rows($sql); //Count the number of rows returned

    if ($userCount == 1) {
        while($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)) {
             $rahul = $row["id"];
        }

        $_SESSION["id"] = $rahul;
        $_SESSION["user_login"] = $user_login;
        $_SESSION["password_login"] = $password_login;
       exit("<meta http-equiv=\"refresh\" content=\"0; url=./home.php\">");
    } else {
        echo 'That information is incorrect, try again';
        exit();
    }
}
?>

i don't know what to add in the mysqli_query to allow user to login from both the username and email.

MKD

simple change your query like this

 $sql = mysqli_query($conn, "SELECT id FROM users2 WHERE (username='$user_login' OR `email`='$user_login') AND password='$md5password_login' LIMIT 1"); // query the person

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Allow login using username or email in Laravel 5.4

Allow user to log in with either Email OR UserName (AspNet.Identity)

Login username AND email in Parse Android

Login with username or email with Cakephp 3

How to login with "UserName" instead of "Email" in MVC Identity?

How to restrict user login in Laravel if email is not verified

How to get current login user's username & email address in C# Azure B2C authentication without Graph API?

Laravel - Login with username, email or phone

How to allow only email as username alias with CloudFormation?

Login with Ion Auth both Username and Email

How to login by email instead of username in spring security

Support login with email or username for same user in ASP.NET Core

ASP.NET Core : How to login with “UserName” instead of “Email”?

User Import: How to allow using the same email address for multiple users but different username?

User login using Email or Username gives null if user not found in ASP.NET Core MVC

how to allow email as well as username to login into account?

Login to get username not email

How to grab username from the PHP Login Project

email as username to login joomla 3.4

How to allow a user to login via Remote Desktop?

How to login user in both WordPress and custom website?

PDO Login System With Username and Email

Firebase login using username (not email)

How to check if both username and email are already registered?

Login user for a custom user model with email as username field

How to create a login but with a username or email and password

Login with email instead of username

Is there any user flow that allow changing the login email?

How to allow both resource server and client login for an endpoint?