2
Hello. I am creating a login system for study purposes.
I identified in the file that is not populating the array with the database information... and yes, I have already made sure that the email and password informed already exists in the database.
Follow login.php file:
<?php
require_once 'init.php';
// resgata dados digitados no formulario
$email = isset($_POST['email']) ? $_POST['email']: '';
$senha = isset($_POST['senha']) ? $_POST['senha']: '';
// Cria o hash da senha
$seg_senha = password_hash($senha, PASSWORD_DEFAULT);
// Verifica se os campos do form nao estao vazios
if(empty($email) || empty($senha)){
echo 'Informe Email e Senha';
exit;
}
// Comando no banco de dados
$pdo = db_connect(); // Abre conexão com o banco
$sql = "SELECT id, nome FROM usuarios WHERE email = :email AND senha = :senha";
// Cria query
$stmt = $pdo->prepare($sql); // Prepare da query
$stmt->bindParam(':email', $email);
// Atribui valor do campo email no valor email da query
$stmt->bindParam(':senha', $seg_senha);
// Atribui valor do campo senha no valor senha da query
$stmt->execute(); // Execute na query
$arr = $stmt->fetchAll(PDO::FETCH_ASSOC); // Cria array associativo
if(count($arr) <= 0){ // Verifica se existe elemento no array
echo "<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='../login.php';</script>";
exit;
}
// Pega o primeiro usuario
$user = $arr[0];
// Inicia a sessão
session_start();
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['nome'];
header('Location: ../index1.php');
If I comment from the line if(count($arr) <= 0){
down, and put a var_dump($arr);
below $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
returns an empty array
C:\wamp64\www\ProjetoALPHA\core\login.php:33:
array (size=0)
empty
Could someone help me?
Grateful!
Do not change the question answered, but invalidate the answer. Below are two different snippets, one to detect wrong password separated from wrong user, which is best for you to test. The other section is to warn of the wrong login, better for the day to day. You can not mix the two, or use one or use the other. The negation is to reverse the test result, so you have to use according to what is inside the if and Else. When in doubt, use as you are, and only adjust after you understand.
– Bacco
Last time, when I answered you, you had pasted a code in Pastebin that I already pointed out the errors and fixed, the problem was that you had mixed things up. We’re here to help, but if you can keep up with the explanations, it’s easier. As for having withdrawn the acceptance, if that’s why the code did not meet, right you, but it is something that discourages us to help here, because if it did not work after all the time I spent with you, great chance that you did not pay attention to what was explained. 'Cause I only left after you confirmed it was okay.
– Bacco
So, if you have something concrete that you can pass, which did not work in the code below, you can even add at the end of your question, or even ask in the comments. but without changing the part that was answered already, otherwise it becomes a mess. And it is no use to fill up with screenshots, because it only complicates the post. If you have to code, format as code (and without removing the original, otherwise it spoils what has been answered and explained).
– Bacco
@Bacco, I did not mix the if conditions, I simply tried one that passed me, it did not work, so I tried the other that had passed me, since the second was different from the first by the sign of denial. And I took the acceptance, because even with your help the code did not work as it should, when I posted this question and told him that it was ok because I had not done the test trying to log in with the wrong password. And when I tried that today, the system was logging in even with the wrong password.
– Gabriel
And I didn’t change the question that was asked, so I entered the equal signs ( === ) to separate the original question from the doubt now
– Gabriel
Try as code snippet instead of screenshot. With stretch is easier for others who are helping too, and if it is extensive already gets scroll. As a screenshot you can’t even copy and paste to test.
– Bacco
Note that this is not my opinion, here is a list of things that the community generally recommends not to do: http://meta.pt.stackoverflow.com/questions/5483/70
– Bacco
I understand. But the print codes are the same ones that are already posted here as code snippets...
– Gabriel