Alert only once Session

Asked

Viewed 590 times

4

I wanted to know how to make an Alert appear only 1 time for the user in the session, in the case the Loga user and appears a div with Alert informed Logged in successfully , after closing this Alert I want him not to show up while the session is open

the code that displays Alert when the user enters the system is this

<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>

1 answer

3


I think what you want in this context is by convention called flash of a message, appears once and 'self-destructs'. That’s how:

When you log in, let’s imagine that you do so (actually how you do it is not relevant, just to understand):

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

Then in html taking advantage of 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, how did the unset of the message that was saved in the session it will only appear once because the next one will no longer enter the if(isset($_SESSION['logged_success'])) {....

  • thanks friend , worked out here

  • You’re welcome @Thalleshonorato. I’m glad I helped

  • I do it with cookies, but next time I’ll use Session, much simpler.

Browser other questions tagged

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