Help with the conclusion of the ban.

$ip = $_SERVER['REMOTE_ADDR']; $file = "ips.dat"; $ban = file($file); $count = count($ban); for ($i=0; $i<$count; $i++) { if($ip == $ban[$i]) { die("I'm sorry, you've been banned. $ip"); } } 

I insert this code at the beginning, but it does not help ... It is necessary that if the if record was executed, if the record was displayed about the bath, if not, then the code would execute the scripts.

  • The file format would not be superfluous. Just addresses, everyone on the line? - ivkremer
  • Yes, everyone on the line ... so it is convenient to write through the form. - ivanforpw 2:41
  • edit the code to read the form, 4 spaces at the beginning of the line or select the code and press the button "101010" - Vladimir Klykov

3 answers 3

 $ip = $_SERVER['REMOTE_ADDR']; $file = "{$_SERVER["DOCUMENT_ROOT"]}/ips.dat"; // у меня файл в корне лежал $ban = file($file); $count = count($ban); for ($i = 0; $i < $count; $i++) { if ($ip == trim($ban[$i])) { die("I'm sorry, you've been banned. $ip"); } } 

Just added trim - it works.

  • echo $ ban [$ i]; <<< should remove this line - Vladimir Klykov
  • Yes, thanks, just testing, I have left. - ivkremer 2:58
 $ip = $_SERVER['REMOTE_ADDR']; $fp = fopen("ips.dat", "r"); $my_ips=''; if ($fp) { while (!feof($fp)) { $my_ips. = fgets($fp, 999); } if (preg_match("/$ip/",$my_ips)!=0) die("I'm sorry, you've been banned. $ip"); }else echo 'Ошибка открытия файла!'; fclose($fp); 

Try it. I like this implementation honestly more than a line by line comparison)

  • and how do they differ? execution speed? - ivanforpw
  • one
    By the way, I thought that this should be faster ... the entire file is not read immediately line by line, but it turned out that the speed of this is even slightly slower if the ip of the banned one is at the end and much slower if at the beginning of the file. For 2000 ip addresses (own ip is the last in the list) it is approximately 0.002 against 0.0017, and if the first line is, then 0.002 against 0.00048 ... If you replace 999 with 40, then it starts working a little faster. - ivkremer
  • differ in implementation, in my approach you can easily clog entire subnets using regulars) for example 192.168.1. {2,4} there will be all ip from 192.168.1.0 to 192.168.1.255 - Vladimir Klykov
  • international x) - ivanforpw

Why reinvent the wheel?

 <?php $ip = $_SERVER['REMOTE_ADDR']; $banlist = file_get_contents('ips.dat'); $search = strpos($banlist, $ip); if ($search !== false) die ("Вы забанены"); ?> 

Everything should work.

  • Yeah, only IP xxx.xxx.xxx.1 will ban a third of the subnet) - Sh4dow
  • Bgggg, ban on ip as evil :) - Sharp- eyed