How to save and read an IP only when the last range of the IP has a difference of 10?

0

I do not know if the question is well formulated, but with the code below, I get the user's IP and save it in a .txt file. and when the user tries to enter again, he reads the .txt file and sends a message saying that his IP has already been used. what I want to do is that if the saved IP is for example: 192.168.1.10, and the user's ip is 192.168.1.15. tell you the same as if the ip was saved, but if your IP is for example: 192.168.1.21. then if you save it and allow you to visit the link. that is to say that the last rank of numbers of the ip, must have a difference of at least 10.

$ip = $_SERVER['REMOTE_ADDR'];
$file = file_get_contents( "ips.txt" );
$archivo = "ips.txt";
$proceso = fopen($archivo, "a");
$datos   = "".$ip."\n";

if( preg_match( "/$ip/", $file ) ) {
    echo "Esta IP ha sido usada recientemente, cambia la IP.";

}
    else{
    header ('Location: https://www.ejemplo.com/?view=ad1');
    fwrite($proceso, $datos);
    fclose($proceso);
}
    
asked by Luis Cesar 13.06.2018 в 12:23
source

1 answer

0

If I understand you correctly, what you need is to remove the last byte from the address, so it would be more convenient if you used a Substring.

$ip = substr($_SERVER['REMOTE_ADDR'], 0, -2);

Edit:

Responding to the question, you would have to use substring again, but now in a different way.

The first thing would be to read the file and check that it is not empty, then check that the new IP is at least 10 digits smaller and then execute the rest of your code.

  $file = file_get_contents("ips.txt");
  if(filesize($file)!=0){
    $file = substr(file_get_contents("ips.txt"), 10, 1);
    $ip = substr($_SERVER['REMOTE_ADDR'], 0, -2);
    $archivo = "ips.txt";
    $proceso = fopen($archivo, "a");
    $datos   = "".$ip."\n";

      if($ip>$file+10){
        if( preg_match( "/$ip/", $file ) ) {
          echo "Esta IP ha sido usada recientemente, cambia la IP.";        
        }
        else{
          header ('Location: https://www.ejemplo.com/?view=ad1');
          fwrite($proceso, $datos);
          fclose($proceso);
        }
      }  
  }
  else{
     echo "archivo vacio";
  }

PS: It should be something similar to this.

    
answered by 13.06.2018 / 12:30
source