Log in to php using $ _SESSION [closed]

0

Could someone put a simple code on how to use the global variable $ _SESSION?

The problem is that when logging in, I do not know how to save the user in the global variable $ _SESSION and how to use it later in the other pages.

    
asked by UnaiLopez 12.11.2017 в 14:35
source

1 answer

1
try {

    $base = new PDO("pgsql: host=localhost; port=5432; dbname=sistema", "postgres", "123456");

    $base->SetAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);

    $pg = "select * from usuarios where usuario = :usuario or correo_usuario = :usuario";

    $resultado = $base->prepare($pg);

    $usuario = htmlentities(addslashes($_POST['usuario']));

    $clave = htmlentities(addslashes($_POST['clave']));

    $resultado->Execute(array(":usuario" => $usuario));

    $registro = $resultado->fetch(PDO::FETCH_ASSOC);

        if(password_verify($clave, $registro['clave'])){

            session_start();

            $_SESSION['login']=$registro;
            header("location:cronograma.php");

        }else{

            session_start();
            $_SESSION['error']=$alerta;
            header("location:acceso.php");

        }

 } catch (Exception $e) {

    die("Error: " . $e->GetMessage());

 }

With this code you can start your session_start , the user is saving in $_SESSION['login'] , to recover the data you need from your logged in user you would have to add another parameter that you already have declared in your database, for example , $_SESSION['login']['id_usuario'] , so you retrieve the id of the user entered. If in your database you have nombre then for the name you would put $_SESSION['login']['nombre'] the value you want to recover you declare it in the second dimension of the array.

    
answered by 12.11.2017 / 15:06
source