Problems with displaying cookies

4

I have a Cookie set with the name 68 and value 68, it appears in the cookies settings in the browser, but on pages it works and in others it simply does not exist

$id_imovel = (isset($_COOKIE[$id_imovel_form])) ? $_COOKIE[$id_imovel_form] : '' ;

if (empty($id_imovel) || $id_imovel == "") {
    setcookie($id_imovel_form, $id_imovel_form, time()+604800);
    $retorno = array('codigo' => 0, 'mensagem' => 'imóvel favoritado com sucesso');
    echo json_encode($retorno);
    exit();
}

$id_imovel_form is 68.

On the real estate page it appears correctly:

foreach ($imoveis as $imoveis) {
    if (isset($_COOKIE[$imoveis->id_imovel]) and $_COOKIE[$imoveis->id_imovel] != ""): ?>
        <div style="background-color: #EAB346; opacity: 0.7;" onclick="favoritarImovel(<?=$imoveis->id_imovel?>,<?=$id_imovel_fav?>)" id="<?=$imoveis->id_imovel?>" class="resp"><img class="inf star" src="../img/estrela.png"></div>
    <?php else: ?>
        <div onclick="favoritarImovel(<?=$imoveis->id_imovel?>,<?=$id_imovel_fav?>)" id="<?=$imoveis->id_imovel?>" class="resp"><img class="inf star" src="../img/estrela.png"></div>
    <?php endif; 
}

On another page with the same code it does not appear, even calling directly by name:

 echo $_COOKIE['68'];
    
asked by anonymous 11.09.2016 / 19:53

1 answer

3

In the setcookie function, specify the path where cookie will be available. Use / to indicate that it works for the whole site and not just the directory where it is being configured:

setcookie($id_imovel_form, $id_imovel_form, time()+604800, '/');
    
11.09.2016 / 20:40