Php setcookie not working

0

I have several files:

  • variables.php: in this I have defined the necessary global variables.
  • checkLogin.php: here I check the user, password, etc. and if the user wants to remember his username and password, I create a cookie with the user, so that:
        $query = "UPDATE usuario SET cookie='".$rand."' WHERE id_usuario=".$row["id_usuario"];
                $result = mysqli_query($conn, $query);
                if ( false===$result ) {
                    die("Conexión errónea: " . mysqli_connect_error());
                    header('Location: ' . $_SESSION['localhost'] . 'Error');
                }
        setcookie("id_usuario", $row["id_usuario"], time()+(60*60*24*365), '/');
        setcookie("marca", $rand, time()+(60*60*24*365), '/');
  • navbar.php (included in the index.html file): here, after doing a header in CheckLogin.php ('Location:' .., I can see the value of the cookies 'user_id' and 'brand'. But nevertheless I can not see a new cookie "test" that I do when entering in index.html, something like:
 
setcookie("prueba", "prueba", time()+(60*60*24*365), '/');
$prueba = filter_input(INPUT_COOKIE, "prueba");

echo "<script type='text/javascript'>alert('Valor cookie prueba: ". $prueba ."');</script>";

but it does not print anything from the $ test variable, I tried everything (ob_start (), ob_end_flush (), create it in test.php, directly with $ _COOKIE ['test']). Does anyone know if the cookie is created when doing a redirect or header: location or can it be created on the same page that will be displayed at the moment? I hope you have explained me well.

    
asked by Pedro Madrigal 03.12.2018 в 22:22
source

1 answer

0

What if you try to get using $ _COOKIE, the code would be like this:

setcookie("prueba", "prueba", time()+(60*60*24*365), '/');
$prueba = $_COOKIE['prueba'];
echo "alert('Valor cookie prueba: ". $prueba ."');"
    
answered by 03.12.2018 в 22:51