Session Management does not validate

0

I have the following problem:

configuration for the server:

ini_set('zlib.output_compression', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.cookie_httponly', 1);

I'm trying to validate if the sessions are active

if(!isset($_SESSION)){
    session_name('PruebaSesion');
    session_start();
    $_SESSION['prueba']='No Existe Sesión, se procede a crearla';
    echo $_SESSION['prueba'];
}else{
    echo 'Sesion Habilitada';
    echo var_dump($_SESSION);
}

I was hoping that when doing soda I printed the second message but it does not keep printing: "No Session exists, we proceed to create it";

    
asked by Francisco Núñez 25.10.2017 в 22:40
source

1 answer

1

To be able to access the session variable you must first call session_start.

You should do something like this:

<?php
session_start();

if(! isset($_SESSION['prueba'])){

    $_SESSION['prueba']='No Existe Sesión, se procede a crearla';
    echo $_SESSION['prueba'];
} else {

    echo 'Sesion Habilitada';
    echo var_dump($_SESSION);
}

Usually a variable $_SESSION['id'] or its equivalent is declared that stores the id or code of the user who has accessed your website.

But before accessing any session variable you should use session_start

Asi:

<?php
session_start();

if(! isset($_SESSION['id'])){

    header('Location: ./login.php');
    exit();
} 
$id = $_SESSION['id'];

// Se consulta base de datos para ver si existe el usuario si no refeccionar al login
    
answered by 26.10.2017 / 02:02
source