$ _SESSION recover data

1

Good, I have an archiv that retrieves the IDs and makes a login for roles so that:

VALIDAR.PHP

<?php
//Fichero que usara para mandar los datos recibidos más abajo para pasarlos por el controlador.
include "../controlador/usuariosControlador.php";
//Recoge los datos de index.php introducidos en el login, verifica que en ambos campos existan datos.
if (isset($_POST["usuario"]) || isset($_POST["pass"])) {
if (trim($_POST["usuario"]) == "" || trim($_POST["pass"]) == "") {
    echo "false";
} else {
    $usuariosCon = new usuariosControlador();
    $usuario = $usuariosCon->validar($_POST["usuario"], $_POST["pass"]);

    if (count($usuario) > 0) {
//Una vez detecta que el usuario existe le indicaremos los datos que debe seguri la SESSION para calificar los roles, es decir, hacia donde debe de dirigir todo el trafigo del log segun su rol.
        session_start();
        $_SESSION["id"] = $usuario["id"];
        $_SESSION["usuario"] = $usuario["usuario"];
        $_SESSION["privilegio"] = $usuario["privilegio"];
        echo "true";
        echo $validacion;
        if ($_SESSION['privilegio'] == 0) {
            header('location: userinv.php');
        } else if ($_SESSION['privilegio'] == 1) {
            header('location: user.php');
        } else if ($_SESSION['privilegio'] == 2) {
            header('location: jefe.php');
        } else if ($_SESSION['privilegio'] == 3) {
            header('location: administrador.php');
        } else if ($_SESSION['privilegio'] == 4){
            header('location: superadmin.php');
        } 
        else {
            echo "false";
        }
    }
}
}
?>

As we see, it redirects the websites that I tell you, and within this one you have options to move around. Well, if you enter one of these, it goes like this:

Menuadmin.php

session_start();
//Iniciaremos la variable SESSION y con ello le indicaremos QUIÉN NO tiene permisos para acceder aqui
 if (!isset($_SESSION["privilegio"]) || $_SESSION["privilegio"] == 0) {
print "<script>alert(\"Acceso invalido!\");window.location='../../index.php';</script>";
}

if (!isset($_SESSION["privilegio"]) || $_SESSION["privilegio"] == 1) {
print "<script>alert(\"Acceso invalido!\");window.location='../../index.php';</script>";
}

if (!isset($_SESSION["privilegio"]) || $_SESSION["privilegio"] == 2) {
print "<script>alert(\"Acceso invalido!\");window.location='../../index.php';</script>";
}

To check that you have permission to access or not. Well once you access this you want to go to your main menu but I can not because the data of your session is as if you have already lost them, nose,

For example, I said, go back to the validar.php file to redirect it to your predefined index but the page remains blank:

<?php echo '<a href="../../validar.php">Menu</a>'; ?></li>
    
asked by Alberto Cepero de Andrés 22.05.2017 в 13:08
source

2 answers

1

When you are returning to Validar.php you check the POST data that is null when you return by clicking the Menu link, add a else to check in case of the data Post be null if there is session data. Here is an example of what I mean:

<?php
//Fichero que usara para mandar los datos recibidos más abajo para pasarlos por el controlador.
include "../controlador/usuariosControlador.php";
//inicia la sesion
session_start();
//Recoge los datos de index.php introducidos en el login, verifica que en ambos campos existan datos.
if (isset($_POST["usuario"]) || isset($_POST["pass"])) {
if (trim($_POST["usuario"]) == "" || trim($_POST["pass"]) == "") {
    echo "false";
} else {
    // comprobar y asignalos datos de sesion
    $usuariosCon = new usuariosControlador();
    $usuario = $usuariosCon->validar($_POST["usuario"], $_POST["pass"]);
    if (count($usuario) > 0) {
        $_SESSION["id"] = $usuario["id"];
        $_SESSION["usuario"] = $usuario["usuario"];
        $_SESSION["privilegio"] = $usuario["privilegio"];
        echo "true";
        echo $validacion;
        //comprueba los privilegios del usuario
        PrivilegioUsuario();
    }
}
}else if(isset($_SESSION["id"], $_SESSION["usuario"], $_SESSION["privilegio"])){
    //este else se ejecuta si no hay datos POST, comprueba que haya datos de sesion y en caso de haberlas, comprueba los privilegios del usuario
    PrivilegioUsuario();
}

//una funcion para evitar repetir el mismo codigo y llamarlo desde dos sitios
function PrivilegioUsuario(){
//Una vez detecta que el usuario existe le indicaremos los datos que debe seguri la SESSION para calificar los roles, es decir, hacia donde debe de dirigir todo el trafigo del log segun su rol.

        if ($_SESSION['privilegio'] == 0) {
            header('location: userinv.php');
        } else if ($_SESSION['privilegio'] == 1) {
            header('location: user.php');
        } else if ($_SESSION['privilegio'] == 2) {
            header('location: jefe.php');
        } else if ($_SESSION['privilegio'] == 3) {
            header('location: administrador.php');
        } else if ($_SESSION['privilegio'] == 4){
            header('location: superadmin.php');
        } 
        else {
            echo "false";
        }

}
?>
    
answered by 22.05.2017 / 13:45
source
0

Good, check the session documentation so you can see how it works.

link

Also, keep in mind that in your code, if I go back to "validar.php" without values in POST (as you do in <?php echo '<a href="../../validar.php">Menu</a>'; ?></li> it will always give you false, because you force yourself to have a POST['usuario'] and POST['pass']

You comment that the page is blank, have you checked with a right click "View source code of the page" that is really blank? You may be printing the echo "false"; .

Does the error log give you an error? Can we see the driver?

    
answered by 22.05.2017 в 13:43