First visit message on the site

Asked

Viewed 164 times

0

I have a system that every time the customer logs in, counts one more visit, and on the first visit a welcome message appears. But if the user reloads the page again or changes tab and goes back to the main page, the welcome message appears again. It only stops appearing if it scrolls down and logs in again, which increases the visitor counter.

I thought to put on the button to close the message a request for the database to consider the message as seen and it does not appear anymore, but I wonder if it can do somehow that the user the message appears and when it changes tab or reloads the page, the message disappears permanently without using the database. It is possible?

  • 2

    What code do you use to determine whether or not the message should be displayed? Is this done on the front end, back end, or both? For now my guess is that you can change the counter of visits immediately after determining that the message should appear, so any subsequent visit does not fall in the message. But if you know the code, you might have a better idea.

  • Use a SESSION with a unique user value, such as their ID or email. When they log in, you display the message and create the SESSION. After that, put a code that checks if SESSION is active, if it is, it does nothing (does not display the message).

2 answers

1

The script below will only run if there is no Session.

if (!isset($_SESSION['visitado'])){ 
   //mensagem boas vindas
   echo "Bem-vindo";
   //script para contabilizar visita
   //cria uma sessão
   $_SESSION['visitado'] = session_id();
}

put the script inside the login validation

0


I got it done here. Thanks for the help, but now that I saw it was easier than I was thinking. I did so:

In validating:

if($nVisitas->visitas >= 1){
        $contagem = $nVisitas->visitas + 1;

        $visitas = $conecta->prepare("update users set visitas = '$contagem' where email = '$email'");
        $visitas->execute();
                    }

On the home page:

if($nVisitas->visitas == 0){
                echo "<p>Bem Vindo ao site!</p>";

                $visitas = $conecta->prepare("select * from users where email = '$email'" );
        $visitas->execute();
        $visitou = $visitas->fetch(PDO::FETCH_OBJ);


        $contagem = $visitou->visitas + 1;

        $visitante = $conecta->prepare("update users set visitas = '$contagem' where email = '$email'");
        $visitante->execute();

        }

This way, the first time the user logs in, will not increase the validation, will only increment on the main page that will give a visit after the message code is already on the screen. So, when I change tab or reload the page, it no longer shows the message as it has already increased a visit. I put it here to help someone if you have the same question.

Browser other questions tagged

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