setcookie does not work

Asked

Viewed 688 times

0

Good I have the following php code that creates a cookie so when I echo this this does not print anything on the screen and the result of the search in DB is fine because it says that the $result has an array and so enters the if.

 $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_emailu, $cookie_email, time() + (86400 * 30), "/"); // 86400 = 1 day


           echo $_COOKIE[$cookie_emailu];
            $rer=0;
      //   echo "<script>   setTimeout(window.location='../',0);</script>"; 



}
}
  • I think you need to refresh the page to appear, the cookie will be created after the next upload;

  • I try to use the cookie on another page and it doesn’t work either, I just don’t think it’s creating the cookie

  • to see if it was created in "Inspect Element > Storage > Cookies", if it does not find it may be that your browser or the S.O. is blocking

  • I usually use only setcookie("data", serialize($data), time() + 3600 * 24 * 365);

  • For me your code is correct, if $cookie_emailu and $cookie_email is a string

  • because not even using $_SESSION works, I want to move things to another page and I can’t

  • If you’re going to use $_SESSION don’t forget session_start(); first of all.

  • I know, I’m starting to think it’s a server problem because this is okay and I’m stuck in a beginner’s problem

  • In case you don’t need to use $_SESSION to move to another page, as you set the Cookie it will be saved in the browser until its time expires or you remove it

  • I know the problem is that it doesn’t get stored in the cookie

Show 5 more comments

1 answer

2

Nothing like a minimal and reduced example. Looking at the documentation of php has two main problems that may happen. Something be printed before the call to setcookie(), causing the call to function to return false. Or it may be that the cookie created is limited to a specific subdirectory (the fourth parameter of the function setcookie() serves to indicate in which directories the cookie will be visible). Starting from these two problems create this folder structure to exemplify:

/(raiz)
    index.php
    DiretorioA
        a.php
    DiretorioB
        b.php

The archive index php. has the following content:

<?php
//Diretorio raiz '/index.php'

if(!isset($_COOKIE['cookie_na_raiz'])){
    $status = setcookie("cookie_na_raiz", 
    'Este cookie foi criado na raiz do site', time()+3600);

    if($status){
    echo 'Cookie criado com sucesso';
    }else{
        echo 'Erro ao criar cookie';
    }
}

var_dump($_COOKIE);

Content of a. php:

<?php
//Diretorio a '/DiretorioA/a.php'

if(!isset($_COOKIE['cookie_na_pasta_a'])){
    $status = setcookie("cookie_na_pasta_a", 
    'Este cookie foi criado na pasta a', time()+3600, '/');

    if($status){
    echo 'Cookie criado com sucesso';
    }else{
        echo 'Erro ao criar cookie';
    }
}

var_dump($_COOKIE);

And finally content of b. php:

<?php
//Diretorio b '/DiretorioB/b.php'

if(!isset($_COOKIE['cookie_na_pasta_b'])){
    $status = setcookie("cookie_na_pasta_b", 
    'Este cookie foi criado na pasta b', time()+3600);

    if($status){
    echo 'Cookie criado com sucesso';
    }else{
        echo 'Erro ao criar cookie';
    }
}

var_dump($_COOKIE);

Run each of the files (no matter the sequence) just to create cookies.

When you access the file index php. (after following the above step) will be printed:

'cookie_na_raiz' => string 'Este cookie foi criado na raiz do site' (length=38)
  'cookie_na_pasta_a' => string 'Este cookie foi criado na pasta a' (length=33)

When accessing the.php file for the second time it will be printed:

'cookie_na_raiz' => string 'Este cookie foi criado na raiz do site' (length=38)
  'cookie_na_pasta_a' => string 'Este cookie foi criado na pasta a' (length=33)

And finally, when accessing the b.php file will be printed:

'cookie_na_raiz' => string 'Este cookie foi criado na raiz do site' (length=38)
      'cookie_na_pasta_a' => string 'Este cookie foi criado na pasta a' (length=33)
'cookie_na_pasta_b' => string 'Este cookie foi criado na pasta b' (length=33)

This shows the difference in visibility according to the domain in which the cookie was created. Reproduce this example and one of the two situations must be happening in your code.

  • says she was raised successfully but when I try to use them on another page she tells me that this one varies

  • var_dump gives me that there are 0 cookies

  • but tell me the cookie was successfully created

  • But the part of being accessible according to the directory?

  • I create the cookie tells me that created successfully and when I var_dump tells me that there are 0 cookies created

Browser other questions tagged

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