|  | Posted by Heiko Richler on 01/24/07 09:44 
Françoise Debat wrote:> <?php
 >
 > //get user's IP address
 > $userip = $_SERVER['REMOTE_ADDR'];
 >
 > //get list of banned IPs from external file
 > $filename="http://www.mysite.com/banned.txt";
 > $bannedlist = array();
 > $file = fopen($filename, "r");
 > while(!feof($file)) {
 >
 >     //read file line by line into a new array element
 >     $bannedlist[] = fgets($file, 4096);
 
 foef is true after the last fgets fails!
 
 >     }
 >     fclose ($file);
 
 what does   print_r($bannedlist);   tell you?
 
 > //check if the current user's IP is in the banned list
 > if (in_array($userip, $bannedlist))
 >     {
 >     //it is, so send him packing
 >     echo "This website is currently unavailable";
 >     exit();
 >
 >     }
 >
 > //continue executing the script
 >
 > ?>
 
 Do not copy the file in an array. Check row by row. Forget about feof.
 
 I would do this:
 
 $file = fopen($filename, "r");
 while($row = fgets($file, 4096)) {
 //read file line by line
 if ($userip == trim($row)) {
 //it is, so send him packing
 echo "This website is currently unavailable";
 exit();
 }
 }
 fclose ($file);
 
 
 Heiko
 --
 http://portal.richler.de/ Namensportal zu Richler
 http://www.richler.de/    Heiko Richler: Computer - Know How!
 http://www.richler.info/  private Homepage
  Navigation: [Reply to this message] |