Php file:

$ipp = $_SERVER['REMOTE_ADDR']; $banlist = file("/ip.txt"); $banlist = implode("\r\n", $banlist); $ban = array($banlist); $count = count($ban); for ($il=0; $il<$count; $il++) { if($ipp == $ban[$il]) { die("Вы забанены"); } } 

Ip.txt file:

 '85.26.184.19', '85.28.184.19' 

This script works if you do not use the ip.txt file, for example:

 ...код... $ban = array('85.26.184.19','85.28.184.19'); ...код... 

but you need to take ipshniki from the file, help me correct the error.

  • The file function already creates an array. $ ban = array ($ banlist); - this is superfluous to me. - Dmitry Alekseevich

2 answers 2

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

I checked it myself - it works :)

    Try this:

     <? $ipp = $_SERVER['REMOTE_ADDR']; $banlist = explode( "\n", file_get_contents( './ip.txt' ) ); echo 'IP: '.$ipp.'<br>'; if ( array_search( $ipp, $banlist ) ) die( 'Вы забанены' ); ?> 

    The ip.txt file (for my code) should be in the folder with the script look like:

     127.0.0.1 ip2 ip3 
    • timka_s, beautifully written) shol, experiment with the end of the string "\ n" - andrew68
    • for example, what can be done with the end of the line? - shol
    • Try this: $ banlist = explode (" \\ n ", file_get_contents ('./ip.txt')); - andrew68
    • does not work, went through all the options with the end of the line - shol
    • Hmm, maybe I wrote wrong IPs, how to write them in the file correctly, do I need to separate them than the thread or just write a column? - shol