Save visitor IP in text file, but how not to save again if already there and how to read?

Asked

Viewed 281 times

2

I used the answer to this question to save the IP in a text file: How to save the IP of those who visited my site in a text file?

The thing is, I want to create a button like "enjoy" so that students enjoy those who they think are the best teachers.

But how to prevent a student who has already enjoyed short again?

I figured having a text file for each teacher, inside a folder, with the number of ips they liked would be a solution. In the database could get very extensive or gives in the same?

I know I can use Facebook plug-ins, for example, but what if the colors of my site don’t match anything on the face or if, for example, that system works on an intranet? Finally I want to know the logic to achieve this goal.

  • 1

    Use a database. It makes no sense to use a file for this.

  • Anyone can enjoy or only registered users?

  • Only those registered.

  • But so, in the case of an article, anyone could enjoy, registered or not.

  • 1

    IP is not used to control these things, Ips change, if it is on the internet it is usually easy to reset the moden and have a new IP to be able to enjoy again, if it is intranet can happen of more than one person use the same computer then only one could vote.

  • I would use the user’s email then, but then I can only control the ones that are registered. And for the articles? I think that’s what’s best, just enjoy who’s registered, after all only with Facebook who has an account...

  • But won’t it get big in the database? Professor "A", emails: x, t, y, z... Professor "B", emails: c, d, y, z...

  • 2

    That’s what databases are made for :)

  • :3 That’s right! I’ll work on it.

  • Using IP is not good. And for that I would use the enshrined pair: session variable (like cookies) and an SGDBR.

Show 5 more comments

2 answers

2


As people have already commented in the comments, you better use something other than IP to mark the vote, but if you still want to check if a particular IP has already voted use something like:

$arquivo = "ips.txt";

function has_voted($ip) {
  // o método file retorna um array com cada linha do arquivo
  $rows = file($arquivo);

  // basta verificar se o ip do usuário já está gravado no arquivo.
  in_array($ip, $rows);
}

if (!has_voted($_SERVER['REMOTE_ADDR'])) {
  vota(); // seu método para votar

  // grava o ip do cara que acabou de votar no arquivo de ips
  // /a/31970/2321
  $ip = $_SERVER['REMOTE_ADDR']  . "\n";
  file_put_contents($arquivo, $ip, FILE_APPEND | LOCK_EX);
}

0

Some considerations:

a) Save your file as json, as it may always use it in other languages. It could be something like this :

{"ipss":["111.111.111.111","222.222.222.222"]}

and save as filename.json. You can also use comma-separated values (CSV), but I prefer json, which I find more descriptive.

b) The more ips you have, the slower it will be and execution of the function, because where we will act is a collection, so as the collection increases, the more data we have to act. Databases are optimized for this.

c) did not include opening and recording the file, as it already knows how to do.

function check_ip($novo_ip)
{
    $sip = NULL;

    //aqui abre o arquivo json
    $ips = '{"ipss":["111.111.111.111","222.222.222.222"]}';

    //verifica se o ip é válido
   if (!filter_var($novo_ip, FILTER_VALIDATE_IP)) 
        return "Não pode votar";

    //passa json para array
    $o = json_decode($ips,TRUE);

    //já votou, não pode votar mais
    if(is_array($o['ipss']) && in_array($novo_ip, $o['ipss']))
        return "Não pode votar";

     $o['ipss'][]="$novo_ip";

     //cria miolo do json
     foreach ($o['ipss'] as $value) 
     {

            $sip .='"'.$value."',";
    }

    //aqui eu coloquei return mas pode colocar para gravar o aquivo atualizado
    return '{"ipss":['.trim($sip,",").']}';

}

Browser other questions tagged

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