Problems when retrieving array with MySQL info

0

I'm trying to login with Classes and Objects.

The thing has been that when using Classes and Objects the register variable that an associative array saves is inaccessible now and it gives me an error,

require_once('conf_db.php');

class BaseDatos{

    protected $con;
    protected $db;
    protected $registro;

    public function conectar() {
        $this->con = mysqli_connect(HOST, USER, PASS, DBNAME);
        if ($this->con == '0') DIE("Lo sentimos, no se ha podido conectar con MySQL: ".mysqli_error());
        //$this->db = mysqli_select_db(DBNAME, $this->con);
        if (DBNAME == '0') DIE("Lo sentimos, no se ha podido conectar con la base datos: ".DBNAME);
        return true;
    }
    public function desconectar() {
        if ($this->conectar->con) {
            mysqli_close($this->$con);
        }
    }

    public function consulta(){
        $sentencia = 'SELECT * FROM Usuarios';
        $result = mysqli_query($this->con, $sentencia);

        while ($fila = mysqli_fetch_assoc($result)) {
            $registro[] = $fila;
        }
    }
}
?>

Now the security file that will check the password and create the session.

<?php 

    include('../include/conexion.php');
    $db = new BaseDatos();
    $db->conectar();
    $db->consulta();

    if($db->conectar()){
        //Datos recogidos de login.php
        $user = $_POST['usuario'];
        $pass = $_POST['contrasena'];
        //Para una mayor seguridad pasamos la contraseña a un hash
        $hash = password_hash($_POST["contrasena"], PASSWORD_DEFAULT);

        foreach ($registro as $registros) {
            $pass_bd = $registros['contrasena'];
            $user_bd = $registros['usuario'];
            if (($user_bd = $user) AND (password_verify($pass_bd,$hash))) {
                session_start();
                $_SESSION['logueado']=TRUE;
                header('Location: control.php');
            }
            else{
                header('Location: login.php');
                echo "Error";
            }
        }
    }
?>
    
asked by sergibarca 31.01.2018 в 12:03
source

2 answers

0

you are not returning the data of the query outside of your function:

try this way:

public function consulta(){
    $sentencia = 'SELECT * FROM Usuarios';
    $result = mysqli_query($this->con, $sentencia);
    $fila = mysqli_fetch_assoc($result);
    return $fila;

}

and in the function of the query, it is a single record that you take out of the database, thus proves the validation:

$datos = $db->consulta();
$pass_bd = $datos['contrasena'];
$user_bd = $datos['usuario'];
if (($user_bd = $user) AND (password_verify($pass_bd,$hash))) {
    session_start();
    $_SESSION['logueado']=TRUE;
    header('Location: control.php');
}
else{
    header('Location: login.php');
    echo "Error";
}

because a foreach is not required for what you are trying to do. By the way, it filters the data that it searches for users for auitentication, something like this:

public function consulta($X){
    $sentencia = 'SELECT * FROM Usuarios WHERE usuario = $X';
    $result = mysqli_query($this->con, $sentencia);
    $fila = mysqli_fetch_assoc($result);
    return $fila;

}

and queries like this:

$datos = $db->consulta($_POST['usuario']);
    
answered by 31.01.2018 в 13:12
0

The failure is that you are not returning the data from your sentence as you have already mentioned, even so, I have modified the function of your query a bit so that it is more useful, so you can work with different sentences and not a single one .

On the other hand, your password_verify is not going to work for you, you're having both parameters and the first one should not be wasted.

Your code:

password_verify($pass_bd,$hash)

Remember, $pass_bd must be wasted in your Database for security and to verify it you must do the following:

//Contraseña usuario.
$pass = $_POST['contrasena'];
//Verificamos contraseña.
password_verify($pass,$pass_bd)

Possible example:

Database

require_once('conf_db.php');

class BaseDatos{

    protected $con;
    protected $db;
    protected $registro;

    public function conectar() {
        $this->con = mysqli_connect(HOST, USER, PASS, DBNAME);
        if ($this->con == '0') DIE("Lo sentimos, no se ha podido conectar con MySQL: ".mysqli_error());
        //$this->db = mysqli_select_db(DBNAME, $this->con);
        if (DBNAME == '0') DIE("Lo sentimos, no se ha podido conectar con la base datos: ".DBNAME);
        return true;
    }

    public function desconectar() {
        if ($this->conectar->con) {
            mysqli_close($this->$con);
        }
    }

    //Pasamos nuestra consulta directamente.
    public function consultaRetorno($sql){            
        $result = mysqli_query($this->con, $sql);    
        return $result;                
    }
}

We check the user

<?php 

    include('../include/conexion.php');
    $db = new BaseDatos();
    $db->conectar();        

    if($db->conectar()){
        //Datos recogidos de login.php
        $user = $_POST['usuario'];
        $pass = $_POST['contrasena'];
        //Para una mayor seguridad pasamos la contraseña a un hash
        $hash = password_hash($_POST["contrasena"], PASSWORD_DEFAULT);

        //Creamos sentencia
        $sql = "SELECT * FROM Usuarios WHERE usuario = '$user'";
        //Pasamos sentencia a nuestra función.
        $result = $db->consultaRetorno($sql);
        //Retornamos datos
        $registro = mysqli_fetch_assoc($result);

        if ($registro['usuario'] == $user) {

            if (password_verify($pass,$registro['contrasena'])) {
                session_start();
                $_SESSION['logueado']=TRUE;
                header('Location: control.php');
            } else {
                echo "Error contraseña";
            }

        } else {
            echo "Error usuario";
        }            
    }
?>
  

Note: I advise you for security use mysqli prepare statements or PDO. I also advise you to read well    How to avoid SQL injection in PHP ?

    
answered by 31.01.2018 в 15:17