I cannot find Login error

Asked

Viewed 125 times

0

am logging in, but even when I put the data of a registered user appears invalid data.

login.php

<?php

session_start();

require_once "Classes/UsuariosDAO.php";
require_once "Classes/UsuariosVO.php";

$objUsuario= new UsuariosVO();
$objBDUsuario= new UsuariosDAO();

$email=$_POST['HTML_email_USUARIO'];
$senha= md5($_POST['HTML_senha_USUARIO']);


$tmpLogin=$objBDUsuario->loginUsuario($email, $senha);

if($tmpLogin==null){
//dados invalidos
echo 'Dados Inválidos';

 }else{
$objUsuario=$tmpLogin;

$_SESSION['email']=$objUsuario->getEmail();
$_SESSION['nome']=$objUsuario->getNome();
$_SESSION['status']= true;
$_SESSION['msg']= "Olá, ". $_SESSION['nome']."!"; 

$rsDados=$objBDUsuario->permissaoUsuario($permissao);
$tblDados= mysql_fetch_array($rsDados);

header("location:Home.php");
}

User

function loginUsuario($tmpEmail, $tmpSenha){

$objBDpi = new BancoDAO();
    $objBDpi->AbreConexao();


$sqlLogin="Select * from usuarios where ";
$sqlLogin.="email_USUARIO like '$tmpEmail' ";
$sqlLogin.=" and ";
$sqlLogin.="senha_USUARIO like '$tmpSenha'";

 $mysqli = new mysqli('localhost', 'root', '', 'bdpi');

$rsLogin= mysqli_query($mysqli,$sqlLogin) or die($mysqli_error($mysqli));

if(mysqli_num_rows($rsLogin)>0){ 

    $tblLogin= mysqli_fetch_array($rsLogin);

    $tmpUsuario=new UsuariosVO();

    $tmpUsuario->setEmail($tblLogin['email_USUARIO']);
    $tmpUsuario->setNome($tblLogin['nome_USUARIO']);

    return $tmpUsuario;


}else{
   return null;

}
}

Usuariosvo.php

<?php
class UsuariosVO{
public $nome, $senha, $email;

function _construct(){
    $this->setNome("");
    $this->setSenha("");
    $this->setEmail("");

}
public function getNome() {
    return $this->nome;
}

public function getSenha() {
    return $this->senha;
}

public function getEmail() {
    return $this->email;
}


public function setNome($tmpNome) {
    $this->nome = $tmpNome;
}

public function setSenha($tmpSenha) {
    $this->senha = $tmpSenha;
}

public function setEmail($tmpEmail) {
    $this->email = $tmpEmail;
}




}
?>

Bancodao.php

<?php

 class BancoDAO {
     public $usuario="root", $senha="";
     public $servidor="localhost", $banco="bdpi";



     function AbreConexao(){
         $mysqli = new mysqli('localhost', 'root', '', 'bdpi');
         mysqli_set_charset($mysqli,'UTF-8');
        // Verifica se ocorreu algum erro
     if (mysqli_connect_errno()) {
       die('Não foi possível conectar-se ao banco de dados: ' .    mysqli_connect_error());
exit();
     }
     }

        function FechaConexao(){
          mysqli_close();
      }
  }

found that the error is between these lines:

 $rsLogin= mysqli_query($mysqli,$sqlLogin) or die(mysqli_error($mysqli)); if(mysqli_num_rows($rsLogin)>0){ tenho certeza. 

Pq if I modify to (mysqli_num_rows($rsLogin)==0 and put an echo "went bad" in place of:

        $objUsuario=$tmpLogin; $_SESSION['email']=$objUsuario->getEmail(); $_SESSION['nome']=$objUsuario->getNome(); $_SESSION['status']= true; $_SESSION['msg']= "Olá, ". $_SESSION['nome']."!"; header("location:Home.php"); 

echo appears "It went bad"

  • 1

    Have you tried using a Try catch to get the Exception of the possible problem?

  • I didn’t try, how do I do it?

  • These links can help: What Try/Catch Blocks are for and when they should be used? and What are Try/Catch blocks? with Try/Catch. Just one question, your password is registered in the bank with MD5 encryption?

  • thanks, I’ll take a look. Yes the password is saved in the bank on MD5

  • You wouldn’t have to Return inside the if instead of echo in Usergo? instead of echo $tmpUsuario; do Return $tmpUsuario; ?

  • even then it was not... And I cannot find the error. Even if the user is registered will still appear invalid data. What will be?

  • Ask the question the code of BancoDAO

  • put the code

  • Ever tried to give a echo $tmpEmail.' - '.$tmpSenha; to know if the values are being received ? Try changing $sqlLogin.="email_USUARIO like '$tmpEmail' "; and $sqlLogin.="senha_USUARIO like '$tmpSenha'"; for $sqlLogin.="email_USUARIO = '$tmpEmail' "; and $sqlLogin.="senha_USUARIO ='$tmpSenha'";

  • I’ve tried, nothing’s changed

Show 5 more comments

1 answer

0

In the command below you have to put a Return in place of echo.

Being as follows:

if($rsLogin->num_rows > 0){ 

$tblLogin= mysqli_fetch_array($rsLogin);

$tmpUsuario=new UsuariosVO();

$tmpUsuario->setEmail($tblLogin['email_USUARIO']);
$tmpUsuario->setNome($tblLogin['nome_USUARIO']);

return $tmpUsuario;

}

Also replace your loginUsuario method below.

function loginUsuario($tmpEmail, $tmpSenha){

$objBDpi = new BancoDAO();
    $objBDpi->AbreConexao();


$sqlLogin="Select * from usuarios where ";
$sqlLogin.="email_USUARIO like '$tmpEmail' ";
$sqlLogin.=" and ";
$sqlLogin.="senha_USUARIO like '$tmpSenha'";

$mysqli = new mysqli('localhost', 'root', '', 'bdpi');

if(mysqli_connect_errno())
    die();

$rsLogin= $mysqli->query($sqlLogin);

if($rsLogin->num_rows > 0){ 

    $tblLogin= mysqli_fetch_array($rsLogin);

    $tmpUsuario=new UsuariosVO();

    $tmpUsuario->setEmail($tblLogin['email_USUARIO']);
    $tmpUsuario->setNome($tblLogin['nome_USUARIO']);

    return $tmpUsuario;


}else{
   return null;

}
}
  • even then it was not... And I cannot find the error. Even if the user is registered will still appear invalid data. What will be?

  • Try replacing your SQL by saying: $sqlLogin="Select * from usuarios Where "; $sqlLogin.=" email_USUARIO = '$tmpEmail' "; $sqlLogin.=" and "; $sqlLogin.=" password_USUARIO = '$tmpNow'";

  • didn’t help :/ Is it some mistake in the bank?

  • @fer I did the test on my machine running your script and ran perfect without any problem. As a test you can do the following, where you take the values coming via html POST put the information you have in your bank, in my case I put $email='[email protected]'; and $password= md5('test');. Sometimes it is not a problem of the PHP code itself can be the way in which you are sent this data via HTML. Let me know the result when you test.

  • Then what will it be?

  • I edited the above message, check again please.

  • Still appears invalid data

  • How are the data in the database? Is the encrypted password correct? Try to put this password in your database 698dc19d489c4e4db73e28a713eab07b and in the password variable you put test. Check if it will pass. Another thing, comment the lines //$objBDpi = new Bancodao(); // $objBDpi->Abreconexao();

  • are encrypted

  • really is encrypted

  • What was the result? Gave Invalid Data again?

  • I wonder what’s wrong?

  • Try the following: Place a var_dump($mysqli); right after the variable $mysqli and another var_dump($rsLogin); right after the variable $rsLogin). Tell me what will be the return of these two var_dump.

  • when you have tested on your machine, you copy exactly what is written in the question?

  • Yes it was the same thing, the only thing I changed was the echo, replaced by the Return as I told you. Another thing I commented on the lines /$objBDpi = new Bancodao(); // $objBDpi->Abreconexao(); and //$rsDados=$objBDUsuario->permissaoUsuario($permissao);

  • even $tmpEmail is all right. Then either the screen appears blank or data invallidos appears

  • I tried to do what you did but it’s still the same

  • The error is in one of these lines: $rsLogin= mysqli_query($mysqli,$sqlLogin) or die(mysqli_error($mysqli)); if(mysqli_num_rows($rsLogin)>0), I’m sure. Pq if I modify to ==0 and put an echo instead of: $objUsuario=$tmpLogin; $_SESSION['email']=$objUsuario->getEmail(); $_SESSION['name']=$objUsuario-get>Nome(); $_SESSION['status']= true; $_SESSION['msg']= "Hello, ". $_SESSION['name']."!" ; header("Location:Home.php"); echo appears

  • I edited my answer try to do the way I put it now and tell me the result.

  • Fatal error: Call to Undefined method mysqli::connect_errno() in

Show 15 more comments

Browser other questions tagged

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