How to log in with your email address or username
I have a login form that works great The user must enter his username and password to connect to my site But I would like to modify the code so that he can connect with his email address as well as with his nickname.
How could I fix this
Thanks for the help
<?php
session_start();
require_once "base.php";
if (!empty($_POST["pseudo"]) && !empty($_POST["password"])) {
$pseudo = !empty($_POST["pseudo"]) ? $_POST["pseudo"] : null;
$password = !empty($_POST["password"]) ? $_POST["password"] : null;
$sql =
"SELECT pseudo, email,id,confirme, password,confirmkey FROM utilisateurs WHERE pseudo = ?";
$datas = [$pseudo];
//Execution de la requete
try {
$requete = $bdd->prepare($sql);
$requete->execute($datas);
} catch (Exception $e) {
// en cas d'erreur :
echo " Erreur ! " . $e->getMessage();
echo " Les datas : ";
print_r($datas);
}
$data = $requete->fetch();
$row = $requete->rowCount();
$userexit = $requete->fetch();
//si le peusdo est bien dans la base alors on continue sinon on sort
if ($data["confirme"] != 1 and $row == 0) {
//pseusdo pas bon
header("Location: connexion-site.php?login_err=divers2");
die();
}
//else if
//($data['confirme'] !=0 and $row == 1)//psuedo bon
//{header('Location: connexion-site.php?login_err=divers3');
// die();}
if ($data["confirme"] != 0) {
if ($row == 1) {
if (password_verify($password, $data["password"])) {
if (empty($_SESSION["link"])) {
header("Location: index.php?pseudo=" . $_SESSION["pseudo"]);
} else {
header("location: " . $_SESSION["link"]);
}
die();
} else {
header("Location: connexion-site.php?login_err=password");
die();
}
} else {
header("Location: connexion-site.php?login_err=already");
die();
}
} else {
header("Location: connexion-site.php?login_err=divers");
die();
}
}
?>
<?php
session_start();
//on recupere le lien link pour la redirection vers les autres pages
$link = !empty($_GET['link']) ? $_GET['link'] : NULL;
require_once 'base.php';
//si le pseudo et le pawword ne sont pas vide
if (!empty($_POST['pseudo']) && !empty($_POST['password']))
{
//je recupere proprement des variables
$pseudo = !empty($_POST['pseudo']) ? $_POST['pseudo'] : NULL;
$password = !empty($_POST['password']) ? $_POST['password'] : NULL;
$email = !empty($_POST['email']) ? $_POST['email'] : NULL;
//pr?�paration de la requ??te et des variables
//$sql = 'SELECT pseudo, email,id,confirme, password,confirmkey FROM utilisateurs WHERE pseudo = ?';
$sql = 'SELECT pseudo, email,id,confirme, password,confirmkey FROM utilisateurs WHERE pseudo = ? OR email = ?';
//$datas = [$pseudo];
$datas = array($pseudo,$pseudo);
//Execution de la requete
try
{
$requete = $bdd->prepare($sql);
$requete->execute($datas);
}
catch(Exception $e)
{
// en cas d'erreur :
echo " Erreur ! " . $e->getMessage();
echo " Les datas : ";
print_r($datas);
}
$data = $requete->fetch();
// $row = $requete->rowCount();
//je teste si le pseudo ou l'email sont bon
if (empty($data))
{
header('Location: connexion-site.php?login_err=MailPseusdoMauvais');
die();
}
//je teste pour savoir si ils sont bien confirme
if ($data['confirme'] != '1')
{
header('Location: connexion-site.php?login_err=ComptePasActive');
die();
}
// si il est confirme je continue les tests
if ($data['confirme'] != 0)
{
//si l 'email ou le pseusdo sont bons on continue
if (!empty($data))
{
//on verifie le pawword si il est ok on continue , sinon sortie
if (password_verify($password, $data['password']))
{
//on definit et on cree les variables sessions
$_SESSION['user'] = array(
'id' => $data['id'],
'pseudo' => $data['pseudo'],
'email' => $data['email'],
'confirmkey' => $data['confirmkey']
);
$_SESSION['id'] = $data['id'];
$_SESSION['pseudo'] = $data['pseudo'];
$_SESSION['email'] = $data['email'];
$_SESSION['confirmkey'] = $data['confirmkey'];
//on test la variable link si elle est vide on va vers index
if (empty($_SESSION['link']))
{
header('Location: ./index.php?pseudo=' . $_SESSION['pseudo']);
//sinon on va faire la derniere page visitee
}
else
{
header('location: ' . $_SESSION['link']);
}
die();
}
else
{
header('Location: connexion-site.php?login_err=password');
die();
}
}
else
{
header('Location: connexion-site.php?login_err=already');
die();
}
}
else
{
header('Location: connexion-site.php?login_err=ComptePasActive');
die();
}
}
?>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.