Delete URL when reloading

2

I'm doing a login, everything went perfectly with PHP, only I have a problem. When a user is not found in the database, PHP returns the following:

header("Location: ../index.php?error=userNotFound");

This parameter I capture with a GET as follows:

<?php
if(isset($_GET['error']))
{
  if($_GET['error']=="userNotFound")
  {
?>
<div class="alert alert-danger fade in">
  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>Acceso denegado.<br/></strong> El usuario y/o la contraseña no existe.
</div>
<?php }}?>

The problem is that when I reload the page with F5 or I give Enter to the URL:

?error=userNotFound

It is not deleted, that causes the alert to reappear and I have to delete it manually.

Can you tell me a way to clean the URL when the page is reloaded?

    
asked by Alberto Siurob 05.08.2016 в 01:48
source

2 answers

4

As you mention Shaz in the comments , you can use sessions for that. The idea would be the following:

  • On the page where if a user is not found, they are redirected to the index with the error message you must: 1) Log in at the beginning; and 2) before doing the redirect, save a session variable with the error.

    The code would be something like this:

    // inicia sesión
    session_start();
    
    // ... cálculos y resto de página ...
    
    // guarda la variable de error y redirecciona
    $_SESSION["error"] = "userNotFound";
    header("Location: ../index.php");
    
  • On the homepage (index): 1) you would check if that session variable is instantiated; and if it is (similar to how you do it now), 2) show the error message and delete the variable (this way it will not be shown when the page is reloaded with F5).

    The code would be something like this:

    // inicia sesión
    session_start();
    
    // ... cálculos y resto de página ...
    
    // si exista la variable de sesión de error
    if(isset($_SESSION['error']) && $_SESSION['error']=="userNotFound")
    {
         // borra la variable y muestra el mensaje de error
         unset($_SESSION["error"]);
    ?>
    <div class="alert alert-danger fade in">
       <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
       <strong>Acceso denegado.<br/></strong> El usuario y/o la contraseña no existe.
    </div>
    <?php 
    }
    
    // ... cálculos y resto de página ...
    
  • answered by 05.08.2016 / 03:26
    source
    3

    Keeping in mind that Alvaro already explained in practice what I commented quickly Regarding the session, I will show the method by means of cookies, only as an alternative (I would use the session method, which also depends in a certain way on cookies):

    In the file where the login is processed, let's say login.php :

    ...
    // asumiendo que la variable contenga un boolean con el resultado de la autenticación
    if ($login) { 
    
        // login exitoso ...
    
    } else {
        setcookie("loginError", 1, time() + 120); // 2 minutos de vida debería ser suficiente
        header("Location: ../index.php');
    }
    

    Then, in the file index.php we check if the cookie exists and show (or not) the error message:

    ...
    if (isset($_COOKIE["loginError"]) && $_COOKIE["loginError"]) {
    
        // si existe, hacemos que expire (en 1 segundo)
        setcookie("loginError", false, 1);
        ?>
        <div class="alert alert-danger fade in">
          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
          <strong>Acceso denegado.<br/></strong> El usuario y/o la contraseña no existe.
        </div>
    
    <?php } ?>
    ...
    

    Again, this is just an alternative method, creating a cookie with such a short "life" is not very useful in most cases.

        
    answered by 05.08.2016 в 13:13