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.
Using Cookies ?
– NoobSaibot
You can help me how ?
– Matheus Vitor
Well, now I’m unable to give an answer because I have no way to test the code, maybe some colleague can help you.
– NoobSaibot
Thank you. Thank you very much.
– Matheus Vitor