Sessions on a login system

Asked

Viewed 136 times

0

I made a login system in PHP where, after authentication, I go back to the page index.php.

So far so good, the problem is that when I redirect the user to the index.php again, it asks me for login data.

Someone knows what I’m doing wrong?

Follow the page index.php:

<?php
if(!isset($_SESSION)){ 
   session_start(); 
} 

if(!isset($_SESSION["$Email"])){
   echo 'todos os forms de login';
} 
else{
   echo 'todo o site';
}

And here is the authentication function:

public function autenticacao($Email, $Senha){
    $oConexao = new conexaoclass();
    $oConexao -> abrirConexao();
    $sql = "SELECT * FROM Professores 
            WHERE Email = '$Email' AND Senha = '$Senha'";

    $this -> resultado = mysql_query ($sql, $oConexao -> getConn());

    $rows = mysql_num_rows($this -> resultado);

    if ($rows > 0) {
        session_start();
        $_SESSION["$Email"] = $Email;
        $_SESSION["$Senha"] = $Senha;

        echo "Login realizado com sucesso! <br> Aguarde, você será redirecionado...";
        echo "<script>login()</script>";
    }
    else{
        echo "Login inválido!!! Dados incorretos! <br> Aguarde, você será redirecionado...";
        echo "<script>loginFail()</script>";
    }
}
  • 1

    Instead of $_SESSION['$email'] use $_SESSION['email'] = $email.

  • 2

    Maybe it’s not the focus of the question. But always try to avoid keeping a pure password in the bank. Try to store the encrypted password and at the time of authentication only compare the encrypted passwords. Security issue. If you are interested here you have a good discussion on the subject: http://answall.com/questions/2402/como-fazer-hash-de-passwords_safesafesafesafety_passwords

1 answer

1


In part $_SESSION["$Email"] take off the $.

You are telling php that you want to use the contents of the variable as the session index $Email, which is not defined in the vector.

Preferably, when using associative arrays, use single quotes '.

Do this with the other similar occurrences in your code.

More information: Strings in php

Browser other questions tagged

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