Store user ip when entering the site

Asked

Viewed 207 times

0

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

$ip = $_SERVER['REMOTE_ADDR'];

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

1 answer

1


To avoid unnecessary traffic I would do this with Cookies, also why the visitor can change IP if you move 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 as well and use COOKIES or LOCALSTORAGE.

Now if you really want to register the Ips, you should create a matching, in case I advise JSON, and when accessing PHP will query all ARRAY ITEMS within the JSON and if you do not find the IP in the list it will display the video.

{"IP_Visitantes":["202.1.x.x","187.2.x.x","190.4.x.x","66.8.x.x",]}

Browser other questions tagged

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