$ _SESSION ["danger / success"] in php to display alerts

0

LogicaUsuario.php

<?php
session_start();
function usuarioEstaLogado(){
    return isset($_SESSION["usuario_logado"]);
};
function verificaUsuario(){
    if (!usuarioEstaLogado()){
        $_SESSION["danger"] = "Você não tem acesso a essa funcionalidade";
        header("Location: index.php");
        die();
    };
};
function usuarioLogado(){
    return $_SESSION["usuario_logado"];
};
function logaUsuario($email){
    $_SESSION["usuario_logado"] = $email;
};
function logOut(){
    session_destroy();
};

index.php

 <?php 
        include("header.php");
        include("logicaUsuario.php");
        if(isset($_SESSION["success"])): 
    ?>
            <div class="alert-box">
                <p class="alert success"><?= $_SESSION["success"] ;?></p>
            </div>
    <?php
        ;elseif(isset($_SESSION["danger"])):
    ?>
            <div class="alert-box">
                <p class="alert error"><?= $_SESSION["danger"] ;?></p>
            </div>
    <?php
        ;endif;
        unset($_SESSION["success"]);
        unset($_SESSION["danger"]);
    ?>

        <div class="container">



        </div>

    <?php include("footer.php"); ?>

Logout.php

<?php
include("logicaUsuario.php");
verificaUsuario();
logOut();
$_SESSION["success"] = "Deslogado com sucesso";
header("Location: index.php");

I'm trying to make sure that when user deslogue appears a message saying that it was successful, however, the message does not appear, I made access restriction to logado/deslogado , and it worked, but I'm not able to with logout

    
asked by anonymous 01.12.2016 / 13:46

1 answer

1

logicaUsuario.php is killing the session in session_destroy(); because this function destroys all data associated with the current session: manual .

Instead of using session_destroy(); , specify which session items you want to destroy and which ones to keep:

function logOut(){
    #session_destroy();
    unset($_SESSION['item0']);
    unset($_SESSION['item1']);
    unset($_SESSION['item0']);
};

Or restart the session with session_start() :

function logOut(){
    session_destroy();
    session_start();
};

Leave set to only $_SESSION["success"] , index.php already has an instruction to destroy it after use.

    
01.12.2016 / 14:15