Alert only once Session

4

I would like to know how to make an alert appear only once for the user in the session, in case the user Loga and appears a div with alert informed Logged in successfully, after closing this alert I want it not to appear any more while the session is Open

The code that displays alert when the user enters the system is

<div class='alert alert-success alert-dismissible' role=alert><b>$logado</b> você foi logado com sucesso  - 
   <a href=logout.php class='alert-link'>Deslogar</a>
  <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button>

   </div>
    
asked by anonymous 03.07.2016 / 07:02

1 answer

3

I think what you want in this context is by convention called flash a message, appears once and 'self-destruct'. It's like this:

When you log in, let's say that you do this (in reality, as you do, it is not relevant, just to notice):

...
$_SESSION['loggedin'] = $user_id;
$_SESSION['logged_success'] = 'você foi logado com sucesso'; // acrescenta a sua mensagem também na sessão
...

Then in html using what you have:

if(isset($_SESSION['logged_success'])) {
    echo "<div class='alert alert-success alert-dismissible' role=alert>" .$_SESSION['logged_success']. "</div>";
    unset($_SESSION['logged_success']); // depois de imprimir o que queremos apagamos esta var da sessão
}

In this case, as did the unset of the message that was saved in the session it will only appear one because the next one will no longer enter if(isset($_SESSION['logged_success'])) {... .

    
03.07.2016 / 12:21