My setcookie doesn’t work

Asked

Viewed 332 times

-2

Well, I have the following code to decide my cookies and use another page, so when I make $_COOKIE[$cookie_email] on the other page where I want to use the value saved gives me whenever this value is null. even if '/' does not work

$nomenecessario=$_GET['ref'];
$count=$_POST['nivelacessos'];
if ($conn->query("UPDATE subditos SET niveis_acesso_id='$count' WHERE 
id='$nomenecessario'") === TRUE){ 
 //Buscar na tabela usuario o usuário que corresponde com os dados digitado 
 no formulário
    $result_usuario = "SELECT * FROM subditos WHERE id='$nomenecessario' 
   LIMIT 1";
    $resultado_usuario = mysqli_query($conn, $result_usuario);
    $resultado = mysqli_fetch_assoc($resultado_usuario);

    //Encontrado um usuario na tabela usuário com os mesmos dados digitado no formulário
    if(isset($resultado)){
        $cookie_email = $resultado['email'];
        setcookie($cookie_email, time() + (86400 * 30)); // 86400 = 1 day
        $_SESSION['usuarioId'] = $nomenecessario;
        $_SESSION['usuarioNiveisAcessoId'] = $resultado['niveis_acesso_id'];
        $_SESSION['usuarioEmail']=$resultado['email'];
        $_SESSION['usuarioSenha']=$resultado['senha'];
        if($_SESSION['usuarioNiveisAcessoId'] == "1"){
            echo $cookie_email;
                $rer=0;
         echo "<script>irpaginafronta()</script>"; 
        }else{
            $rer=1;
                        echo "<script>irpaginah()</script>"; 

                    }


}
}
  • setcookie($cookie_email, time() + (86400 * 30)), "/"); If we specify "/" the cookie will be valid within the entire domain.

  • has already been answered here https://answall.com/questions/152224/problemas-para-displayingcookies

  • 2

    Possible duplicate of Problems to display cookies

  • none of your answers were helpful as even if '/' does not work

  • You can inform if observed the Developer Tools the cookie has actually been saved?

  • in your case the "do_cookie name" was missing, see reply

Show 1 more comment

1 answer

0

Creating a cookie with php is very simple, the basic creation syntax is:

  • setcookie(name, value, expire, path, domain);

Example 1

  • setcookie("nome_do_cookie", "valor_do_cookie");

The above cookie will be "deleted" automatically when you close your Browser

To create a valid cookie for 30 days we do:

setcookie("name_do_cookie", $cookie_email, time()+(86400 * 30));

in your case "nome_do_cookie"

Example 2

setcookie("Nome", "Music Lyrics HQ", time()+60*60*24*365, "/dir/php/", "www.dominio.com"); 

In the example above we create a cookie called "Name" with the value "Music Lyrics HQ" The cookie expires in 1 year (60 seconds * 60 minutes * 24 hours * 365 days) and can be read only by files in the directory "/dir/php/" in the (sub-)domain "www.dominio.com".

Example 3

Use / to indicate that it works for the entire site and not just the directory where it is being set up:

setcookie('nome_do_cookie', $cookie_email, time() + (86400 * 30), "/"); // 86400 = 1 day

Example 4

setcookie("nome", "Music Lyrics HQ", time()+60*60*24*365, "/dir/php", "www.dominio.com", "1");

In the example above we create a cookie called "Name" with the value "Music Lyrics HQ" The cookie expires in 1 year (60 seconds * 60 minutes * 24 hours * 365 days) and can be read only by files in the directory "/dir/php/" in the (sub-)domain "www.dominio.com" and with a secure connection, because we use the value 1 indicating that the cookie is safe if used 0 he would be insecure.

setcookie

Browser other questions tagged

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