How to tell if a person has visited a page in the last 2 days

2

I have an idea, to show an ad only to those who have visited the site before, and another ad for people who have never visited before .. (LAST 2 DAYS)

SOMETHING LIKE IT:

<?php if ($visitou = sim): ?>
  APARECE ANUNCIO PRA QUEM JA VISITOU NAS ULTIMOS 2 DIAS
<?php else: ?>
  APARECE PRA QUEM NÃO VISITOU NOS ULTMIOS DOIS DIAS, (VAI SER ANUNCIO DIFERENTE)
<?php endif; ?>
    
asked by anonymous 13.10.2018 / 23:16

2 answers

2

You create a cookie using setcookie("nome_do_cookie", "valor_do_cookie", validade_do_cookie)

To verify:

if(isset($_COOKIE['visitante'])) {
    if(!($_COOKIE['visitante'] === null)) {
        //O usuário já acessou seu site e as 48h não acabaram
        echo "O cookie é valido!";  
    } else {
        //O usuario já visitou seu site e as 48h acabarram
        echo "O cookie é invalido.";
    }
} else {
    // o cookie não existe
    // significa que o usuário nunca acessou ou apagou o cookie.
    setcookie("visitante","sim", time() + 172800);
}

No isset ensures that you will not access a null index. In if , you check if the cookie exists, if it exists then it means that the person has visited your site before.

no time () you get the current time, and at 172800 it is the time in milliseconds equivalent to 48 hours.

Of course, a cookie is easy to handle. You can try to get as much information as possible from the user (such as ip, browser, etc ...) and save somewhere, to check later, if the business rule needs this.

    
13.10.2018 / 23:49
0

If the cookie does not exist it shows the advertisement of who did not visit and creates the cookie valid for 2 days. While the cookie is valid, it displays the corresponding ad.

<?php 

if(isset($_COOKIE['visitou'])){
    $visitou="block";
    $naoVisitou="none";

}else{

    $cookie_name = "visitou";
    $cookie_value = "sim";
    setcookie($cookie_name, $cookie_value, time() + (86400 * 2), "/"); // 86400 = 1 dia
    $visitou="none";
    $naoVisitou="block";

}

?>


<div style="display:<?php echo $visitou ?>">Visitou- cookie válido</div>

<div style="display:<?php echo $naoVisitou ?>">Não Visitou cookie expirado</div>
    
14.10.2018 / 00:51