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

Asked

Viewed 434 times

2

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

SOMETHING LIKE THAT:

<?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; ?>

  • Using Cookies ?

  • You can help me how ?

  • Well, now I’m unable to give an answer because I have no way to test the code, maybe some colleague can help you.

  • Thank you. Thank you very much.

2 answers

2


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

To check out:

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);
}

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

in the team() you take the current time, and in the 172800 is the time in milliseconds equivalent to 48 hours.

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

0

If the cookie does not exist, it shows the ad of the visitor and creates the cookie valid for 2 days. As long as the cookie is valid displays the corresponding advertisement.

<?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>

Browser other questions tagged

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