Problems to display cookies

Asked

Viewed 151 times

4

I have a Cookie set with the name 68 and value 68, it appears in the settings of cookies in the browser, but in 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 has value 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, until calling directly by name:

 echo $_COOKIE['68'];
  • Exactly worth

  • I put as a response to make it more organized.

1 answer

3


In function setcookie indicate the path where the cookie will be available. Use / to indicate that it works for the entire site and not just the directory where it is being set up:

setcookie($id_imovel_form, $id_imovel_form, time()+604800, '/');

Browser other questions tagged

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