User not found in PHP login with BD

Asked

Viewed 312 times

1

I am trying to log into the system using PHP database but even using the correct email and password, it does not connect. I believe you really can’t find the record, since it goes through the query but not the following if.

Follow the Login.class:

<?php
class Login{

    public function logar($email, $senha){
        $buscar=mysql_query("SELECT * FROM usuario WHERE email='$email' AND senha='$senha' LIMIT 1");

        if(mysql_num_rows($buscar) == 1){

            $dados=mysql_fetch_array($buscar);

            $_SESSION["email"]= $dados["email"];
            $_SESSION["senha"]= $dados["senha"];

            setcookie("logado",1);
            $log=1;

        }
            if(isset($log)){

                $flash="Logado com sucesso";

            }else{

                if(empty($flash)){
                $flash= "Digite seu e-mail e sua senha corretamente!"; //Se peço para retornar o $email ele retorna.

                }
            }
            echo $flash;

    }

} ?>

And the login.php

<?php
    if($startaction == 1 && $acao == "logar"){

    //Dados
    $email= ($_POST["email"]);
    $senha= sha1($_POST["senha"]); //$senha=addslashes(sha1($_POST["senha"]."ProjetoY"));


    if(empty($email) || empty($senha)){
        $msg="Preencha todos os campos!";

    }else{
        if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
            $msg="Digite seu e-mail corretamente!";

        }else{
            //Executa a busca pelo usuario
            $login=new Login;
            echo "<div class=\"flash\">";
            $login=$login->logar($email, $senha); 
            echo"</div>";

        }
    }} ?>
  • 2

    And what can we do to help you, other than warn you that your code is vulnerable to attack?

  • 2

    Makes it vulnerable!!!

  • I know about vulnerabilities and I even have ideas to make a better security, but at first it’s just to show a login system. rs

  • where is your connection to the bank?!

  • Dude this is college work, these days a guy came up with difficulty with that same code! hahaha

  • Look what they are teaching in the faculties. It must be Systems Technology :P

  • Password is not encrypted in the bank?

Show 2 more comments

1 answer

0

Its class, read and improved, there are several ways to improve security, but as the subject is not that:

<?php
    class Login {

        private $conexao;

        public function __construct() {
            $this->conexao = mysql_connect('localhost', 'root', '') or die("Erro na conexão!");
            mysql_select_db('banco', $this->conexao);
        }

        public function logar($email, $senha) {
            $buscar = mysql_query("SELECT * FROM usuario WHERE email= '".addslashes(trim($email))."' AND senha= '".addslashes(trim($senha))."' LIMIT 1", $this->conexao);

                if(mysql_num_rows($buscar ) == 1) {
                    $dados = mysql_fetch_array($buscar);
                    $_SESSION["email"]= $dados["email"];
                    $_SESSION["senha"]= $dados["senha"]; // qual a necessidade de guardar a senha em sessao???

                    setcookie("logado",1);
                    $log=1;
                }

                if(isset($log)) {
                    $flash = "Logado com sucesso";
                } else { 
                    if(empty($flash)) {
                        $flash= "Digite seu e-mail e sua senha corretamente!"; //Se peço para retornar o $email ele retorna.
                    }
                }
                echo $flash;
        }
    }
?>
  • Thanks, but you still have the same problem. = / I can post DB.class if it helps.

  • debug your step code in step, focus the connection in a file only better still, you can take it out what I did, var_dump() on the returned variables, if you still can not put your code all that we try to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.