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";
}
}
}
?>