PHP - Login System

Asked

Viewed 318 times

0

I am creating a login system and would like to know what is causing the errors

Notice: Undefined variable: e in C: xampp htdocs login CLASSES usuarios.php on line 14

Fatal error: Uncaught Error: Call to a Member Function getMessage() on null in C: xampp htdocs login CLASSES usuarios.php:14 Stack trace: #0 C: xampp htdocs login index_vacc.php(57): Usuario->connect('project_login', 'localhost', 'root') #1 {main} thrown in C: xampp htdocs login CLASSES usuarios.php on line 14

The index is:

<?php
    require_once 'CLASSES/usuarios.php';
    $u = new Usuario();
?>

<html lang="pt-br">
<body>

    <div class="container">
        <div class="content first-content">

            <div class="first-column">
                <h2 class="title title-grey">Olá, bem vindo(a) à plataforma</h2>
                <h2 class="title title-grey">de prestação de contas.</h2>

            </div>

            <div class="second-column">
                <h2 class="title title-white">Entrar</h2>
                <p class="description">Faça o login para o acesso</p>
                <form method="POST">
                    <label class="label-input" for="">
                        <i class="far fa-envelope icon-modify"></i>

                        <input type="email" name="email" placeholder="   E-mail">
                    </label>
                    <label class="label-input" for="">
                        <i class="fas fa-lock icon-modify"></i>

                        <input type="password" name="senha" placeholder="   Senha">
                    </label>
                    <button type ="submit" class="btn btn-grey">login</button>
                </form>

            </div>
            </div>
    </div>
            <?php  
                if(isset($_POST['email']))
                {
                    $email = addslashes($_POST['email']);
                    $senha = addslashes($_POST['senha']);


                    if(!empty($email) && !empty($senha))
                    {
                        $u->conectar("projeto_login", "localhost", "root", "");
                    if($u->msgErro == "")
                        {
                        if($u->logar($email,$senha))
                        {
                            header("location: AreaPrivada.php");
                        }
                        else
                        {
                            echo "Email e/ou senha estão incorretos!";
                        }
                        }
                        else
                        {
                            echo "Erro: ".$u->msgErro;
                        }
                    }
                    else
                    {
                        echo "Preencha todos os campos!";
                    }
                }
            ?>
</body>

</html>

And the part of connection functions:

<?php  

Class Usuario
{
    private $pdo;  
    public $msgErro = "";

    public function conectar($nome, $host, $usuario, $senha)
    {
        global $pdo;
        global $msgErro;
        try {
            $pdo = new PDO("mysql:dbname=".$nome.";host=".$host, $usuario, $senha);
        } catch (PDOException $e) {
            $msgErro = $e->getMessage();
    }
    }
    public function logar($email, $senha)
    {
        global $pdo;
        $sql = $pdo->prepare("SELECT id_usuario FROM usuarios WHERE email = :e AND senha = :s");
        $sql->bindValue(":e", $email);
        $sql->bindValue(":s", md5($senha));
        $sql->execute();

        if ($sql->rowCount() > 0)
        {
            $dado = $sql->fetch();
            session_start();
            $_SESSION['id_usuario'] = $dado['id_usuario'];
             return true;

        }
        else
        {
            return false;
        }
    }

}
?>

I’ve tried to declare a public $e but apparently it didn’t work.

1 answer

0


The error shown is stating that it is not possible to access the method getMessage of a null object, in this case the variable $e because there was an error that prevented the correct capture of the exception that occurred in $pdo = new PDO("mysql:dbname=".$nome.";host=".$host, $usuario, $senha);

To correct: You need to use the $this keeping the reference to the class itself, together with -> to be able to access class variables/attributes within methods conectar and logar, as the variables: $pdo and $msgErro which were declared at the beginning of the class.

And you will NOT need to use global variables as there were here:

global $pdo;
global $msgErro;

For you can access them as I explained above using $this->nomeDaVariavelDaClasse.

The error that was occurring with global variables is linked to the reference to global variables, which can assume unexpected values in the way they were being used here. To see more information about global variables if you need to use in the future (in this case you do not need to): Scope of variables

Possible modifications using the $this->nomeDaVariavelDaClasse in File usuarios.php:

<?php  

Class Usuario
{
    private $pdo;  
    public $msgErro = "";

    public function conectar($nome, $host, $usuario, $senha)
    {
        try {
//aqui use $this->
            $this->pdo = new PDO("mysql:dbname=".$nome.";host=".$host, $usuario, $senha);
        } catch (PDOException $e) {
//aqui use $this->
            $this->msgErro = $e->getMessage();
    }
    }
    public function logar($email, $senha)
    {
//aqui use $this->
        $sql = $this->pdo->prepare("SELECT id_usuario FROM usuarios WHERE email = :e AND senha = :s");
        $sql->bindValue(":e", $email);
        $sql->bindValue(":s", md5($senha));
        $sql->execute();

        if ($sql->rowCount() > 0)
        {
            $dado = $sql->fetch();
            session_start();
            $_SESSION['id_usuario'] = $dado['id_usuario'];
             return true;

        }
        else
        {
            return false;
        }
    }

}
?>

With these modifications will now be displayed the error message if any problem occurs to connect to BD. :)

Browser other questions tagged

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