$_SESSION["Danger/Success"] in php to display alerts

Asked

Viewed 595 times

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 that as a user deslogue, there is a message saying that it was successful, however, the message does not appear, I did with access restriction to logado/deslogado, and it worked, but I’m not getting through logout

  • 1

    In the archive logicaUsuario.php not of unset in the success ?

  • 1

    Someone must be killing the session before printing.

  • I added user logic to the question. @Shutupmagda, I tried to pass session_destroy after the message, but it still didn’t work

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 you want 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();
};

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

  • and how I would do that ?

  • echo $_SESSION["success"]; and then unset($_SESSION["success"]).

  • I refer to specifying which are destroyed and which remain

  • I edited the answer ;)

  • Done, thank you :3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.