I am performing a login, and the user type is a different object, that object in the code that queries if it works fine, use a var_dump to the session variable where I used it and it works fine, but when I send it it no longer works.
Login.php
<?php
session_start();
require_once '../Modelo/PDOConex.php';
if((!$nameUser = trim($_POST['user'])) || (!$password = md5(trim($_POST['pass']) ) ) ){
header('location:../');
}
try{
$stmt = $db_con->prepare("SELECT
idUsuarios,
Cuenta,
Contra
FROM
Usuarios
WHERE
Cuenta=:usuario");
$stmt->execute(array(":usuario"=>$nameUser));
$fila = $stmt->fetch(PDO::FETCH_ASSOC);
if($fila['Contra']==$password){ //Credenciales correctas
require_once 'Log/getTipo.php';
switch (getTipo($fila['idUsuarios'], $db_con)){
case 'Administrativo':
require_once 'Log/LoginAdmin.php';
$_SESION['Usuario'] = serialize(logAdmin($fila['idUsuarios'], $db_con));
$_SESION['Tipo']= 'Administrativo';
break;
case 'Docente':
require_once 'Log/LoginDocente.php';
$_SESION['Usuario'] = logDocente($fila['idUsuarios']);
$_SESION['Tipo']= 'Docente';
break;
case 'Estudiante':
require_once 'Log/LoginEstud.php';
$_SESION['Usuario'] = logEst($fila['idUsuarios']);
$_SESION['Tipo']= 'Estudiante';
break;
case 'Acudiente':
require_once 'Log/LoginAcud.php';
$_SESION['Usuario'] = logAcud($fila['idUsuarios']);
$_SESION['Tipo']= 'Acudiente';
break;
default:
echo "0";
break;
}
}else{
echo '0'; // Credenciales incorrectas
}
}catch(PDOException $e){
echo $e->getMessage();
}
?>
index.php
<?php
require_once 'Controlador/Usuarios/Acudientes.php';
require_once 'Controlador/Usuarios/Administrativos.php';
require_once 'Controlador/Usuarios/Docentes.php';
require_once 'Controlador/Usuarios/Estudiantes.php';
session_start();
// Error does not receive the object, here var_dump prints false...
$_SESSION['Usuario'] = unserialize($_SESSION['Usuario'])
if(isset($_SESSION['Usuario'])){
header('location:Pages/login.php');
}else{
switch($_SESSION['Tipo']){
case 'Administrativo':
if(count($_SESSION['Usuario']->getId_Colegio())>1){
header('location:preAdmin.php');
}else{
header('location:indexAdmin.php');
}
break;
case 'Docente':
if(count($_SESSION['Usuario']->getId_Colegio())>1){
header('location:preDocente.php');
}else{
header('location:indexDocente.php');
}
break;
case 'Estudiante':
header('location:indexEstudiante.php');
break;
case 'Acudiente':
header('location:indexAcudiante.php');
break;
default:
header('location:Controlador/logout.php');
break;
}
}
What am I doing wrong?
After executing the line that it puts
$_SESSION['Usuario'] = unserialize($_SESSION['Usuario']);
, the value treated as the content of the session variable will be saved,$_SESSION['Usuario']
overwriting the original value, so the next time you go through that same line it willunserialize
returnfalse
because it will not be able to work a second time with some data already treated, also generating a type notificationE_NOTICE
(which you may be filtering depending on your PHP configuration).You should use a temporary variable different from the session one (so as not to modify and corrupt its content) to work with the data returned by
unserialize
:Also, you don't always use
serialize
to save the data in such a session variable, so you should make your code homogeneous before proceeding or use a different session variable that always contains the same content type.Minimal functional example:
$numero
As you can see, each time we load the page the class property is incremented by oneUsuario
. That's because the state of the class is saved in the session variable.