← Desenvolvimento

Cookie para fechar DIV

Lida 3324 vezes

Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Boa tarde.

Estou a usar o seguinte código para mostrar uma div, que, ao clicar no "X" se fecha. No entanto não tem nenhum cookie que diga que quando faço refresh à página não volte a abrir, dentro de 10 dias ou 5h.
Código: [Seleccione]
<div id="popjan">

<a style="float:right; margin-bottom: 22px; margin-right: -22px; z-index: 5px;" href="#" onclick="document.getElementById('popjan').style.display='none';"> <img src="x.png" width="22" height="22" /></a>

<div style="float:center; width:980px; margin-bottom: 10px;"><a href="http://ad.pt" target="_blank" onclick="document.getElementById('popjan').style.display='none';"><img src="a7.png" width="980px;" height="100px" /></a></div>


<div style="clear:both"></div>
</div>

Aliado a isso tem também algum CSS básico.

A minha dúvida agora é como adicionar o tal cookie.
Se alguém puder ajudar :)

Abraço ;)
Offline

Diogo Pinto 
Administrador
Mensagens 4400 Gostos 371
Feedback +4

Troféus totais: 37
Trófeus: (Ver todos)
Avatar Search Level 6 Linux User Mobile User Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes

Então metes algo do género (não me lembro da síntese da PHP, segue a lógica):

"SE NÃO EXISTE COOKIE"
CÓDIGO
"SE EXISTE COOKIE COM MENOS DE 10 DIAS OU 5 HORAS"
NÃO FAZ NADA

Percebeste? :P
Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

yup, isso é o que tenho de fazer, depois de ter o cookie criado. Agora preciso que, ao clicar na cruz para fechar, crie o cookie :P
Offline

betanalista 
Membro
Mensagens 2564 Gostos 0
Feedback +18

Troféus totais: 26
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1

isso é sintaxe dos if... else do php

http://www.w3schools.com/php/php_if_else.asp
Offline

diogoosorio 
Membro
Mensagens 134 Gostos 1
Feedback +1

Troféus totais: 22
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

Não testei, mas não deves ter grandes problemas.

Certifica-te que o 1º trecho de código é colocado antes de o que alguma coisa seja impressa no browser:

Código: (php) [Seleccione]
<?php
$show_ad = ($_COOKIE['show_ad'] > strtotime("now")) ? false true;
if($show_adsetcookie('show_ad'strtotime('+5 hours'), strtotime('+5 hours'));
?>


<?php if($show_ad): ?>
<div id="popjan">
<a style="float:right; margin-bottom: 22px; margin-right: -22px; z-index: 5px;" href="#" onclick="document.getElementById('popjan').style.display='none';"> <img src="x.png" width="22" height="22" /></a>
<div style="float:center; width:980px; margin-bottom: 10px;"><a href="http://ad.pt" target="_blank" onclick="document.getElementById('popjan').style.display='none';"><img src="a7.png" width="980px;" height="100px" /></a></div>
<div style="clear:both"></div>
</div>
<?php endif; ?>
Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Não testei, mas não deves ter grandes problemas.

Certifica-te que o 1º trecho de código é colocado antes de o que alguma coisa seja impressa no browser:

Código: (php) [Seleccione]
<?php
$show_ad = ($_COOKIE['show_ad'] > strtotime("now")) ? false true;
if($show_adsetcookie('show_ad'strtotime('+5 hours'), strtotime('+5 hours'));
?>


<?php if($show_ad): ?>
<div id="popjan">
<a style="float:right; margin-bottom: 22px; margin-right: -22px; z-index: 5px;" href="#" onclick="document.getElementById('popjan').style.display='none';"> <img src="x.png" width="22" height="22" /></a>
<div style="float:center; width:980px; margin-bottom: 10px;"><a href="http://ad.pt" target="_blank" onclick="document.getElementById('popjan').style.display='none';"><img src="a7.png" width="980px;" height="100px" /></a></div>
<div style="clear:both"></div>
</div>
<?php endif; ?>
Muito obrigado Diogo :grin:, a ideia é precisamente essa, mas o teu código cria o cookie assim que a publicidade é mostrada, e o que eu necessito é que eu clicar no "x" (é a primeira parte do código que mostrei), seja criado o cookie.
Esse mesmo "x" fecha a publicidade, daí eu querer que o cookie seja criado nessa altura :)

Obrigado também ao Diogo e ao betanalista :)
Offline

diogoosorio 
Membro
Mensagens 134 Gostos 1
Feedback +1

Troféus totais: 22
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

Toma:

Código: (php) [Seleccione]
<?php
$show_ad 
= ($_COOKIE['show_ad'] > strtotime("now")) ? false true;
?>

<?php if($show_ad): ?>
<script type="text/javascript">
function criaCookie(){
var data = new Date();

// Define quantas horas o anúncio é escondido
// Esconde durante 5 hora
data.setHours(data.getHours() + 5);
var segundos = Math.round(data.getTime() / 1000);

document.cookie = "show_ad=" + segundos + ";expires=" + data.toUTCString();
document.getElementById('popjan').style.display='none';
}
</script>
<div id="popjan">
<a style="float:right; margin-bottom: 22px; margin-right: -22px; z-index: 5px;" href="javascript:criaCookie();"> <img src="x.png" width="22" height="22" /></a>
<div style="float:center; width:980px; margin-bottom: 10px;"><a href="http://ad.pt" target="_blank" onclick="document.getElementById('popjan').style.display='none';"><img src="a7.png" width="980px;" height="100px" /></a></div>
<div style="clear:both"></div>
</div>
<?php endif; ?>

Tendo a página já sido carregada, o cookie tem de ser criado via javascript. Para alterares o período em que a div é escondida, altera:

Código: (javascript) [Seleccione]
data.setHours(data.getHours() + 5);
O 5 pelo número de horas que queres esconder. Se quiseres funcionar com dias, substitui essa linha por:

Código: (javascript) [Seleccione]
data.setDate(data.getDate() + 10);
Neste caso esconde durante 10 dias.
Offline

Thomato 
Membro
Mensagens 4125 Gostos 3
Feedback +2

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Bem, já passou mais de um mês desde a tua resposta, mas passou-me mesmo ao lado. Não tinha visto que a tinhas publicado.
É precisamente isso que preciso, funcionou perfeitamente com a integração necessária.
Muito obrigado ;)