How to send an error message to the user correctly?

Asked

Viewed 63 times

0

Hello I am a student of PHP and would like to know from you more experienced the following, if send the error message to the user through a $_SESSION and after that message has been "viewed" the session is destroyed, it would be good practice. and if it is not, how can I do it?

My Example:

Login simulation:

 else{
    //2.1- se der erro no passo anterior mostrar o erro
    //criar uma sessão para mandar a mensagem na index
    $_SESSION['erro'] ='Preencha os campos Corretamente!';
    header('location:index.php');
    }

Now this is the page that gets the message:

<?php 
if (isset($_SESSION['erro'])) {
  echo "<div class='card-panel yellow darken-4'>".$_SESSION['erro']."</div>";
  session_destroy();
}?>
  • The way you did, meets this specific situation, but think about the possibility of working with other errors on your system, after the user logged in. Or the possibility of you using the session to save other things. , it is recommended to delete only what you are not using in the variable, you can use the unset($_SESSION['error']) command that will destroy only this specific variable.

  • Let me get this straight, so in this case, if the user is already logged in to the system, I can also use the $_SESSION['erro'] to send other messages, however as I can reuse it in other instances instead of destroying it, I can just "empty it" using the unset() ..

No answers

Browser other questions tagged

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