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.