My setcookie does not work

-2

Well, I have the following code to set my cookies and use another page, so when I do $ _COOKIE [$ cookie_email] on the other page where I want to use the stored value it always gives me this value is null. even putting '/' 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>"; 

                    }


}
}
    
asked by anonymous 24.12.2017 / 01:00

1 answer

0

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

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

Example 1

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

The above created cookie will be automatically deleted by closing the Browser

To create a valid cookie for 30 days we do:

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

  

In your case, "nome_do_cookie" is missing

Example 2

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

In the example above we created 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 " / dir / php / "in the (sub-) domain" www.domain.com ".

Example 3

Use / to indicate that it works for the whole site and not just the directory where it is being configured:

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 created 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 " / dir / php / "in the (sub-) domain" www.domain.com "and with a secure connection, since we used the value 1 which indicates that the cookie is safe if it was used 0 it would be insecure

setcookie

    
24.12.2017 / 20:37