Store user ip when entering site

0

I want to display a welcome video on my website only when it is the first time the user enters my site, I thought about using the following code:

$ip = $_SERVER['REMOTE_ADDR'];

It returns me the ip of the user and so I would store in the database and every time someone enters the site will do a search if they already had this ip, otherwise it displays the video, but would this be the correct form? In time the search would be very slow and heavy? How can I best resolve this if it is not the most viable?

    
asked by anonymous 06.02.2018 / 12:09

1 answer

1

To avoid unnecessary traffic I would do this with COOKIEs, also because the visitor can change IP if he changes from his internet provider:

<?php
if (!isset($_COOKIE['firsttime']))
{
    setcookie("firsttime", "no", /* EXPIRE */);
    /* Coloque aqui o conteúdo da primeira visita */
}
else
{
    /* OPCIONAL: Coloque aqui conteúdo pras visitas
    seguintes  do mesmo usuário*/
}
?>

You can do this by JAVASCRIPT too and use COOKIES or LOCALSTORAGE.

Now if you really want to register the IPs, you should create a backup, in the case I advise JSON, and when accessing PHP will consult all the ITEMS in ARRAY inside the JSON and if it does not find the IP in the list it will present the video.

{"IP_Visitantes":["202.1.x.x","187.2.x.x","190.4.x.x","66.8.x.x",]}
    
06.02.2018 / 12:29